From 7554d5fef3f478ca0f9c8911522d7c1783989eae Mon Sep 17 00:00:00 2001 From: Paul Mackerras Date: Mon, 18 Dec 1995 03:30:47 +0000 Subject: [PATCH] Update from NeXT-ppp-2.2-0.4.6 --- NeXT/INSTALL | 730 +++++++++++++++++ NeXT/Load_Commands.sect | 5 + NeXT/Makefile | 85 ++ NeXT/Makefile.top | 199 +++++ NeXT/NeXT_Version | 6 + NeXT/README.NeXT | 168 ++++ NeXT/TODO.NeXT | 42 + NeXT/Unload_Commands.sect | 1 + NeXT/bsd-comp.c | 1075 ++++++++++++++++++++++++ NeXT/if_ppp.c | 1633 +++++++++++++++++++++++++++++++++++++ NeXT/if_pppvar.h | 104 +++ NeXT/inlines.h | 243 ++++++ NeXT/linedisc.h | 72 ++ NeXT/nbq.h | 97 +++ NeXT/netbuf.h | 41 + NeXT/ppp_tty.c | 1218 +++++++++++++++++++++++++++ NeXT/random.c | 52 ++ NeXT/random.h | 14 + NeXT/spl.h | 111 +++ NeXT/vjcompress.c | 584 +++++++++++++ README.NeXT | 26 +- pppd/Makefile.NeXT | 16 +- pppd/sys-NeXT.c | 9 +- 23 files changed, 6525 insertions(+), 6 deletions(-) create mode 100644 NeXT/INSTALL create mode 100755 NeXT/Load_Commands.sect create mode 100755 NeXT/Makefile create mode 100644 NeXT/Makefile.top create mode 100644 NeXT/NeXT_Version create mode 100644 NeXT/README.NeXT create mode 100644 NeXT/TODO.NeXT create mode 100755 NeXT/Unload_Commands.sect create mode 100644 NeXT/bsd-comp.c create mode 100644 NeXT/if_ppp.c create mode 100644 NeXT/if_pppvar.h create mode 100644 NeXT/inlines.h create mode 100644 NeXT/linedisc.h create mode 100644 NeXT/nbq.h create mode 100644 NeXT/netbuf.h create mode 100644 NeXT/ppp_tty.c create mode 100644 NeXT/random.c create mode 100644 NeXT/random.h create mode 100644 NeXT/spl.h create mode 100644 NeXT/vjcompress.c diff --git a/NeXT/INSTALL b/NeXT/INSTALL new file mode 100644 index 0000000..db657cb --- /dev/null +++ b/NeXT/INSTALL @@ -0,0 +1,730 @@ +Installation Instructions +========================= + +Files, information, and an FAQ are available from: + http://www.thoughtport.com:8080/PPP/ +Full source and an MAB installer package are available. If you don't +currently have access to a WWW viewer, you may get the files from +ftp.thoughtport.com (199.171.224.105) in the directory /pub/next/ppp/. + +If you have questions or problems, please mail +nextppp@listproc.thoughtport.com _before_ you go to usenet. + +The procedure for an initial installation and for an upgrade are very +similar. However, if this is the first time you are installing PPP, +there are a few extra steps that you must do for the initial setup. +They will be described later in this document. + +Making the source files +========================= + +To install on a NeXT: + + 1) Read this file completely through before you start. + + 2) If you are on an HP-PA system, read the file + ./NeXT/hppa/README.hppa and install the serial + driver patch. Successfull installation of this + patch will require a reboot of your machine. + + 3) If you have previously installed a SLIP package, comment out + the slip configuration code in /etc/rc.local and reboot your + machine. SLIP and PPP _should_ interoperate but for initial + testing it is best to remove SLIP. + + 4) If you plan in using tcpdump to monitor packets, you must + enable the Berkley Packet Filter code. Edit ./NeXT/Makefile + and make sure the '-DNBPFILTER' option is defined in DFLAGS. + WARNING: If you choose to use BPF, you must make sure that you + compile _AND LOAD_ the bpf_reloc LKS _BEFORE_ you load the + ppp_reloc LKS. See the ./NeXT/bpf directory for more information. + + If you don't want to compile in this code, please make sure that + '-DBPFILTER' is _not_ defined in the ./NeXT/Makefile DFLAGS. + + Also, make sure you read the installation file in ./NeXT/bpf. You + will need to create a few special device files in /dev/. + + 5) Type ./configure in the top level PPP directory. This will set + up some necessary links. + + 6) If you have developer 3.2, you need to use the old version of + chat.c. Perform: + a) cd to the chat directory + b) backup chat.c (mv chat.c chat.c.orig) + c) use the old chat (mv chat.c.3.2 chat.c) + + 7) Edit ./Makefile and set the installation directories. + If you change the ETCDIR you will need to modify pathnames in + pppd/pathnames.h. I recommend keeping it set to /etc/ppp. + If you don't specifically like that directory, you may + also change the Makefile directory and put a link in /etc/ppp + that points to the proper place. + + You will also want to set the -arch flags to the appropriate + architectures. + + + 8) Do a: + make + + 9) If you are satisfied with the results, then as root, type: + make install + + +If you are performing an upgrade, you are done after successfully +making and installing the latest release. All you need to do is to +reboot your machine so that the new loadable kernel server (LKS) is +loaded. Make sure you remember to edit /etc/rc.local and add the code +to load the bpf_reloc LKS (if you enabled BPF support). You should be +able to start the upgraded PPP just like normal. If this doesn't +work, see the "Troubleshooting" section later on. + + +Extra Steps for Initial Configuration +===================================== + +If this is the initial installation of PPP, there are several system +administration steps that must be performed. These only need to be +done once. These steps do not need to be re-performed for an upgrade. + +1) Before PPP can successfully run, a module called the loadable + kernel server (LKS) must be linked into the system. This is + something that must be done each time the computer boots up. + + So that you don't have to do this by hand each time the machine + boots, you should modify a file called /etc/rc.local. Since + this is a system file, you must be root to perform the + modification. + + This file contains code that is run each time the machine is + started. This is the standard place where "local" modifications + are made to the system. First, make a backup copy of /etc/rc.local + (maybe named /etc/rc.local.prePPP). It will be available in case + you accidentally mess up the file. Then, using vi or your favorite + editor, place the following lines (not includeing the ==...== + separators ;) somewhere near the end of the file /etc/rc.local: + +====================================================================== +# +# Load the Berkley Packet Filter LKS +# This must be done before PPP. Comment this out +# if you disabled BPF. If you use BPF, you _must_ +# load it before the PPP LKS. If you chaged pathnames +# in the Makefile, make sure the appropriate paths are +# used below. +# +if [ -f /usr/local/bpf/reloc/bpf_reloc ]; then + /usr/etc/kl_util -a /usr/local/bpf/reloc/bpf_reloc +fi + +# +# Load the selected version of the PPP-2.2 loadable +# kernel server (LKS). +# +if [ -f /usr/local/ppp/reloc/ppp_reloc ]; then + /usr/etc/kl_util -a /usr/local/ppp/reloc/ppp_reloc > /dev/console 2>&1 + (echo -n ' ppp') > /dev/console +fi +====================================================================== + + This code will now be executed next time you reboot your + machine. You can verify that this was executed by checking + the output of /usr/adm/messages upon a successful reboot. There + should be a section of output that says the PPP-2.2 LKS was + successfully loaded. + + +2) The default Makefile paths place PPP files in a directory called + /usr/local/ppp. There are several subdirectories under this + directory. However, these directories are not part of the standard + UNIX Path. The UNIX Path is a list of directories that UNIX searches + when it is trying to find a command. There are two solutions + to fix the problem. You may either add specific PPP directories to + the current path (must be done for each individual user), or you + may add important files to a directory that is already in the + standard path. I believe the second approach is the better + solution. + + This step is optional, but highly recommended. As root, + execute the following commands to add important files + to directories that are already in the Standard UNIX path: + + /bin/mkdirs -o root -g wheel -m 755 /usr/local/bin /usr/local/man/man8 + ln -s /usr/local/ppp/bin/* /usr/local/bin + ln -s /usr/local/ppp/man/man8/* /usr/local/man/man8 + + Some or all of the 'mkdir' commands may fail if the directories + already exists. This is OK. Futher, to get the man program to + understand that you have added some pages to the /usr/local/man + directory, you need to make sure that the environment variable + MANPATH includes the /usr/local/man entry. In my .cshrc file + I have an entry that looks like: + setenv MANPATH "/usr/local/man:/usr/man:.:.." + See 'man man' for more information. + + Once these commands are executed, the programs pppd, pppstats, and + chat (along with their respective man pages) will become available + to you from the command line. However, before you can immediately + see them, you may need to log out and log back in. + + +3) Once you start trying to make PPP connections, it is important + to have access to the logging information that PPP generates. This + will allow you to follow the progress of PPP and will aid in + diagnosing problems. The user level process 'pppd' outputs + logging information by using the standard UNIX syslog facility. + Part of this facility allows you to select how much (i.e. + what level of verbosity) and where (i.e. to which file) this + information will be placed. While the following step is optional, + it is highly recommended. + + As root, make a backup copy of /etc/syslog.conf. You may wish + to call it /etc/syslog.conf.prePPP. If you run into problems with + the system logging error messages, you can replace /etc/syslog.conf + with the original, reboot, and then you should be back to normal. + + Now, as root, use vi or your favorite editor to edit the file + /etc/syslog.conf. You need to add the line: + + local2.debug /usr/adm/ppp2.2.log + + It is _imperative_ that you place a character + between the level "local2.debug" and the file name + "/usr/adm/ppp2.2.log. Do _not_ use spaces. If your + editor converts tab characters to spaces, you need to + use a different editor. Also beware of cutting and pasting + between buffers. Sometimes a tab will be converted to spaces + during that operation. Below is the actual contents of + my /etc/syslog.conf: + +====================================================================== +local2.debug /usr/adm/ppp2.2.log +*.err;kern.debug;auth.notice /dev/console +kern.debug;daemon,auth.notice;*.err;mail.crit /usr/adm/messages +mark.debug,daemon.info /usr/adm/messages +lpr.debug /usr/adm/lpd-errs +mail.info /usr/spool/mqueue/syslog + +*.alert;kern.err;daemon.err operator +*.alert root + +*.emerg * +====================================================================== + + Once you have modified /etc/syslog.conf, you then need to perform + one more step. You need to actually create an empty logging file. + This step is necessary because if syslog does not see the file, it + will not create it. So, removing the file is a handy way to turn + off the logging. To create an empty logging file, as root execute: + touch /usr/adm/ppp2.2.log + + Upon a successful reboot, logging will be enabled for pppd + (remember to specify the 'debug' option to pppd to get reasonable + information sent to the logging file). + + +4) If you have a standalone machine, it is necessary for you to make + sure your system is configured to correctly support a network. The + following steps will help in correctly configuring your machine. + Please note, if you do not have a standalone machine (i.e. you are + on a local network), you need to speak with your system + administrator to determine the proper configuration for your + machine. The following only applies to standalone machines. + + It is suggested that you reboot your machine _before_ you start + these procedures. This is to verify that you successfully + completed the previous steps and that your machine does indeed + reboot. If it doesn't, you should replace /etc/rc.local with the + backup copy and reboot. Then, determine why your modifications + failed. After successfully rebooting, you are ready to move on to + these steps. + + As root, make a backup copy of /etc/iftab. You might name it + /etc/iftab.prePPP. Then, modify this file by adding the line: + + lo0 inet 127.0.0.1 netmask 0xff000000 -trailers up + + right before the -1- entry that already exists in the file. The + finished file should look similar to (minus the ---- separators): + ---------------------------------------------------------------------- + + lo0 inet 127.0.0.1 netmask 0xff000000 -trailers up + -1- inet -HOSTCONFIG- + * inet -AUTOMATIC- netmask -AUTOMATIC- -trailers up + + ---------------------------------------------------------------------- + Please note that the file probably has a bunch of comments + at the top (lines that start with #). I left those out in the + example above so I could save space. They of course, can remain + in your copy. + + As root, make a backup copy of /etc/hostconfig. You might + name it /etc/hostconfig.prePPP. If, after the following steps, you + run into problems booting your machine, you may replace + /etc/hostconfig with the backup and you should be able to + successfully reboot. + + Start up the HostManager.app (under /NextAdmin). After + successfully entering the root password, you should get the + HostManager Menu. Select the "Local..." menu option to get the + main window. Modify it as follows: + + For "NetInfo Binding", select "use local domain only" + and make sure the box for "Readable only by local net" + is checked. + + For "Hostname", fill in your host name as appropriate. Do + _not_ add the domain name. For example, use "sidney" + rather than "sidney.cps.msu.edu". + + For "NIS Domain Name", select "None" + + For "Internet Address", use the provided IP address + (probably 127.0.0.1) unless your system administrator has + assigned you your own unique IP address. In this case, + reverify that you are a standalone machine. + + For "Broadcast Address", select "Default" + + For "Time Standard", select "Ignore Network Time" + + For "Netmask", select "Default" + + For "Router", select "None" + + When this is complete, click the "Set" button. You will then have + the option to reboot or to skip the reboot. Choose the appropriate + action. It is suggested that you reboot immediately and verify that + your system does indeed reboot (and behave as normal). If it + doesn't, replace /etc/hostconfig with the backup copy and then + reboot. Once you are back up, determine the problem with your + configuration. + + Please note that this description is generic and will work in most + cases. There are lots of variations that people can and will need + to make. Your network administrator will be able to provide you with + the correct details for your particular site. + + +Congratulations! You have successfully installed PPP and are now +ready to start up a connection. See the section "Initial Testing" for +steps to verify that PPP works on your system. + + +Initial Testing +=============== + +One of the most notoriously difficult portions of getting PPP links up +and running involves writing the script that automatically dials your +modem, connects to the peer, and starts the remote ppp process. Once +you are connected to the peer, each PPP process will start +communications and things become much easier. + +Before you dive into script writing, there is a simpler solution that +will allow you to test the ppp portion. Once this works, getting the +dial scripts to work is a matter of sheer determination! + +The mechanism is this... use a communications package (tip or kermit +are good choices) to manually dial the modem and log into the remote +server. There, manually start up the pppd process (the remote +process, once started, will probably print some garbage on the screen. +You can ignore this). Once this is done, you can exit the +communications process (to free up the device it is using). Then, +start your local pppd on the same device. The pppd processes will +then start communicating. The premise is that you manually perform +the operations that you would like your dial script to automatically +perform. Once you _know_ ppp works, you can spend time on the dial +scripts. + +Please note, you _must_ exit from your communications program before +you start your local pppd. For example if you are in kermit and you +start the peer pppd process by hand, you must completely exit kermit +before you start pppd. If you don't, the connection will not form. +Further, if you find that when you exit, your modem immediately hangs +up, you need to instruct the modem to ignore DTR. There is an AT +command that will do this (AT&D on Supra), but you will need to check +your modem manual to determine the correct command. + +If you are planning on using kermit, Stephane I. Matis +, has supplied this excellent definition +that you may place inside your .kermrc file to help with PPP testing: +---------------------------------------------------------------------- +set term byte 8 + +# define pppd +define pppd - + !pppd < \v(line) > \v(line) defaultroute +---------------------------------------------------------------------- + +To use this, add the above to your ~/.kermrc file (minus the '---...---' +separators). Then start kermit. After you have started the remote PPP +server by hand, return back to your local kermit prompt and execute +the command 'do pppd'. In this particular circumstance, you will not +need to exit from kermit. + +Before you start initial testing, you may want to read the pppd man +page. This will allow you to familiarize yourself with the some of +the options available to you for starting your local pppd. + +Of particular interest for most people is the 'defaultroute' option to +pppd. If you have a standalone machine, then all your foreign traffic +must go to the peer. Adding the 'defaultroute' option to pppd +instructs pppd to set your system up in such a manner. + + +Determining if the link is actually up +====================================== + +There are several ways to determine if the link has actaully been +established. I will go through some of them. + +1) You may look at the pppd log file (typically + /usr/adm/ppp2.2.log). If you see lines that look similar to: + + Jan 11 23:13:38 sidney2b pppd[2141]: local IP address 35.9.12.55 + Jan 11 23:13:38 sidney2b pppd[2141]: remote IP address 35.9.10.13 + + You are probably up. + +2) You may check the status of the PPP interface. Using the command: + /usr/etc/ifconfig ppp0 + + You should see that the interface is UP and that there are valid + IP addresses assigned to it (0.0.0.0 is not valid). Here is an + example of what you might see: + + ppp0: flags=51 + inet 35.9.12.104 --> 35.9.10.14 netmask ff000000 + +3) You may check the routing. When the connection comes up, you + should get at least one route to the new interface. If you + specified 'defaultroute' to pppd, you should also see a default + route. The command for checking routes is 'netstat -rn'. Here + is an example of what you might see: + + Routing tables + Destination Gateway Flags Refs Use Interface + 127.0.0.1 127.0.0.1 UH 2 558 lo0 + 35.9.10.14 35.9.12.104 UH 0 0 ppp0 + default 35.9.10.14 UG 2 25 ppp0 + 192.42.172 192.42.172.1 U 6 1939 en0 + + + In the above case, the peer is 35.9.10.14 and my local machine has + been assigned 35.9.12.104. All foreign traffic goes through the + default route to the peer. + + If you don't have an ethernet card installed on your system, you + will not have an 'en0' interface. + + +The routing issue is important. Discussion of this issue is outside +the scope of these instructions, but I thought it might be beneficial +to list a few other important tools that may help you out. The man +pages can give more details: + /usr/etc/ping - send packets to an IP address or hostname + traceroute - Show the route to a particular machine + +The IP address that you use can be negotiated automatically in PPP. +Unlike SLIP, you do not have to specify an IP address when the link is +brought up. If no address is specified as an argument to pppd, then +PPP will negotiate the address with the peer. This is the preferred +mechanism of operation. Probably the only time you should specify an +IP address as an argument to pppd is if you are assigned your own IP +address by your system administrator. Otherwise, sit back and let PPP +do the work for you. + + +A Typical PPP Session +===================== + +A tyipcal PPP session begins when you log into your system. From a +terminal window, you will run your dial script by typing its name at +the prompt. If you use the Workspace manager, you can double click on +the scripts icon. This will start the chat process that will dial the +modem and log into the remote system. It will then turn control over +to pppd. If your script is successful (as described in the +"Determining if the link is actually up" section), you will be all +set. All your apps, OmniWeb, FTP, telnet, etc should work. + +Once you are through using the connection, you can close down the PPP +link by executing the ppp down script. Again, this can be done by +typing the name of the pppdown script in a terminal window, or double +clicking on the appropriate icon in the Workspace manager. At this +point, ppp will terminate the phone connection and pppd will die off. + +You may start and kill the ppp session as many times as you like while +you are logged on. However, if you do not kill the PPP session, it +will not die once you log out. Thus your telephone will remain off +the hook and your computer will remain connected to the net until you +log back in and shut down the connection. Turning off the computer +will obviously close the connection. + + +Name Resolution +=============== + +Once you have a ppp connection up, you may notice that your machine +will not be able to resolve the names of machines to their IP +addresses. You can check this by trying to telnet to a machine outside +your local domain. If "telnet " is successful, +but "telnet " is not, then your name resolution is not +configured correctly. The fix is to edit the file /etc/resolv.conf. +This file contains two important items. The first is your domain +name. This is the name that is automatically tacked on to a computer +name if you don't specify the complete name. For example if my domain +is 'cps.msu.edu' and I say 'telnet sidney', the computer will try +'telnet sidney.cps.msu.edu' (although, it will not print this name on +the command line). + +The second thing is a list of name servers. These should be local to +your ppp provider. Your network administrator will be able to provide +you with the appropriate addresses. There are default name servers to +use in case the local provider is not responding. An example file +might look like: + +---------------------------------------------------------------------- +domain cps.msu.edu + +# +# Insert local name servers here +# + + +# +# MSU name servers +# +nameserver 35.8.2.41 +nameserver 35.8.2.41 +nameserver 128.247.160.56 + +# +# Other servers -- hopefully something will work if needed +#shadooby.cc.umich.edu +nameserver 35.1.1.91 +#ns.nasa.gov +nameserver 128.102.16.10 +---------------------------------------------------------------------- + +You will need to reboot your computer for the new nameservers to take +effect. + + +Making startup and shutdown scripts +=================================== + +By this time, I'm assuming that PPP has been successfully installed. +However, there are a few more steps that you must perform so that +using PPP (now and with future upgrades) will be convenient and easy. +These steps are optional, but they are highly recommended. + +Make scripts 'pppup' and 'pppdown' that bring up and shutdown PPP +connections. There are a number of example scripts that you can copy +and modify. Reading the man page for 'chat' will help you understand +these scripts. You should note that once these scripts are made, you +probably won't need to change them for future upgrades to PPP. + +One note that you should be VERY careful about. These file are shell +scripts. This means that the contents are executed in a shell just as +if you had typed them in by hand. You must make sure that any +characters that are treated specially by the shell (such as < > | \ ) +are inside quotation marks (""). Otherwise, they will be interpreted +by the shell in a manner that is probably to your dislike. For +example, if your peer sends you a prompt like MSUnet> you must add it +to the chat portion of your script like "MSUnet>". + +In order to ensure that these scripts are not removed or modified when +new versions of PPP are installed, you should copy all important +scripts into a new directory. I suggest /usr/local/ppp/scripts. This +directory will not be modified during installation. Further, as you +did earlier for the important binaries, you may want to add important +scripts to a directory that is in the default UNIX Search Path. The +mechanism for doing this is as follows. Suppose you have a script +'pppup' (found in /usr/local/ppp/scripts) that you want to be +available on your command line. You could execute the commands: + /bin/mkdirs -o root -g wheel -m 755 /usr/local/bin + ln -s /usr/local/ppp/scripts/pppup /usr/local/bin + +Once you log out and log back in, this script will be available. If +you want to be able to call this script from a non-root account, you +need to modify the permission on the file to make it suid root. +Please note that this can be a potential security hazard. See your +system administrator for more details. + +As mentioned above, of particular interest for most people is the +'defaultroute' option to pppd. If you have a standalone machine, then +all your foreign traffic must go to the peer. Remember to add the +'defaultroute' option to pppd in your startup scripts if necessary (or +place it in the /etc/ppp/options file). + +Troubleshooting +=============== + +The NeXT port of ppp-2.2 is stable. However, the PD ppp-2.2 is +considered to be in alpha release. If you have problem, please mail +me before you go to usenet. I'm especially +interested in hearing your suggestions and comments about the package +and related documentation. + +Any time that you have a bug report to send, please include your +hardware type and the LKS version number in all reports. This number +may be found in the file /usr/adm/messages (once the LKS has been +installed). Also, for most questions, it is best to append a copy of +the /usr/adm/ppp2.2.log file. + +There are some common problems that people have. I have listed them +here: +====================================================================== +1) pppd bombs out with an error similar to: + Jan 26 14:46:25 localhost pppd[256]: Connected... + Jan 26 14:46:26 localhost pppd[256]: ioctl(PPPIOCGUNIT): Inappropriate ioctl for device + Jan 26 14:46:26 localhost pppd[256]: Exit. + +This is typically the result of using the wrong LKS with pppd. Some +versions of PPP installed the LKS (ppp_reloc) in +/usr/lib/kern_loader/ppp and some in /usr/local/ppp/reloc. The second +is going to be the standard place for installation from now on. Make +sure that your /etc/rc.local is loading the correct version of the LKS +and make sure you are really calling the correct pppd for use with the +LKS that you loaded. + +====================================================================== + +2) PPP works fine, but when the link is up, netinfo sleeps when you + try to print or send mail. + +You'll see this problem if you don't have a route from your "hostname" +to localhost. In essence, your host doesn't know how to "get back to +itself". My guess is that you didn't follow the installation +instructions and you chose an IP address different than 127.0.0.1. +There is a fix! ;) Do this in your rc.local: + +route add your_host_name localhost 0 + +(with the appropriate substitution of your_host_name) Without it, +netinfo will sleep when trying to connect to the local domain. You +will need to reboot or perform this command manually (as root) for it +to take affect. + +Many thanks to: + Scott Bender + Pete French + Terence Parr +for help on this one. + +====================================================================== + +3) People who are trying to set up a NeXT as a PPP server that + they dial into often complain that they can make a connection, but + the remote machine can only ping the server. No other packets + work. + +Check out the 'proxyarp' option to pppd. Servers (connected to their +LAN) must proxyarp for the remote address (i.e. the address of the +machine dialing in). Further, read the file ./NeXT/README.NeXT (not +this file) for more information on proxyarp. + +====================================================================== + +4) Your pppd/chat dials the modem but you cannot get a negotiation + to start. The /usr/adm/ppp2.2.log file shows something similar + to: + + Mar 13 12:02:41 crystal pppd[243]: sent [LCP ConfReq id=0x1 ] + Mar 13 12:02:44 crystal pppd[243]: sent [LCP ConfReq id=0x1 ] + Mar 13 12:02:47 crystal pppd[243]: sent [LCP ConfReq id=0x1 ] + Mar 13 12:02:51 crystal pppd[243]: sent [LCP ConfReq id=0x1 ] + Mar 13 12:02:54 crystal pppd[243]: sent [LCP ConfReq id=0x1 ] + Mar 13 12:02:57 crystal pppd[243]: sent [LCP ConfReq id=0x1 ] + Mar 13 12:03:00 crystal pppd[243]: sent [LCP ConfReq id=0x1 ] + Mar 13 12:03:03 crystal pppd[243]: sent [LCP ConfReq id=0x1 ] + Mar 13 12:03:06 crystal pppd[243]: sent [LCP ConfReq id=0x1 ] + Mar 13 12:03:09 crystal pppd[243]: sent [LCP ConfReq id=0x1 ] + Mar 13 12:03:12 crystal pppd[243]: LCP: timeout sending Config-Requests + Mar 13 12:03:12 crystal pppd[243]: Connection terminated. + Mar 13 12:03:12 crystal pppd[243]: Serial link is not 8-bit clean: + Mar 13 12:03:12 crystal pppd[243]: All received characters had bit 7 set to 0 + +This is a common problem. It is typically the result of a failure to +properly start the remote PPP process. Make sure you add the '-v' +option to chat (in your dial script) and then check the output of +/usr/adm/ppp2.2.log to see why chat failed to start the remote PPP +process. + +====================================================================== + +5) Your LCP sessions get into an infinite loop while trying to + negotiate the magic numer. Alternately, every LCP echo you send + is accompanied with a message saying that "our own echo reply was + received". I'm not sure why some systems have this trouble, but + the solution is to turn off magic number negotiation at your end. + Add the '-mn' flag to pppd. + +====================================================================== + +6) NXHosting applications over PPP fails. I don't know why this + happens. Rest assured, that I and others do have it working. + The most obvious things to check are that you have your system set + up as a public window server. Also, make sure that you reset the + nmserver in /etc/ppp/ip-up (see the example ip-up file). If it + still doesn't work, you might want to add your peer to your + /etc/hosts.equiv file. I don't know if that has anything to do + with it or not, but I don't know why some people can't NXHost + and others can. + + +Updates +======= + +You may also want to join the mailing list for PPP. This will keep +you informed of new releases and will provide an arena for discussing +problems with the NeXT specific PPP port. To add yourself to the list +(or for any other administrative requests), send an email message to: + listproc@listproc.thoughtport.com +with no subject and message body consisting of: + subscribe nextppp Stephen J. Perkins +(please use your own name ;). To send mail to all the participants on +the list, address your messages to: + nextppp@listproc.thoughtport.com + +If you want announcements only, there is a second "announcements only" +list. To subscribe to this, you may use the alternate body: + subscribe nextppp-announce Stephen J. Perkins +You don't need to subscribe to both. All announcements are forwarded +to the regular list. + +Security +======== + +Security issues are not dealt with in this document. Please +note that the pppd file is installed suid root. This is a potential +security hazard. + + + +Noted bugs +========== + +There have been various problems reported when trying to install LKSs +the way NeXT intended (i.e. placing them in /usr/lib/kern_loader/* and +modifying /etc/kern_loader.conf). The main problem seems to be that +after modifying /etc/iftab, /etc/rc.net (on NSFIP) will not correctly +configure the interfaces. This means that booting under NSFIP will +hang. So, for the time, it is suggested that you install the package +manually and load the LKS in /etc/rc.local. + + +Modem Configuration +=================== + +It is important to use hardware flow control if you use a high speed +modem. On my supra V.32bis modem, the command to use Hardware Flow +Control is 'AT&K3'. + +Also, you will probably want to set your modem so that when the DTR is +dropped, the modem will disconnect. On my modem the command is +'AT&D2' This setting disables auto-answer so if you want to allow +dialins, you must read your modem manual to determine the correct +setting. + diff --git a/NeXT/Load_Commands.sect b/NeXT/Load_Commands.sect new file mode 100755 index 0000000..d701060 --- /dev/null +++ b/NeXT/Load_Commands.sect @@ -0,0 +1,5 @@ +# Associate ports with proc/arg + +CALL pppattach 0 +WIRE +START diff --git a/NeXT/Makefile b/NeXT/Makefile new file mode 100755 index 0000000..243e816 --- /dev/null +++ b/NeXT/Makefile @@ -0,0 +1,85 @@ +# +# Makefile for NeXT OS 3.2 +# +# $Id: Makefile,v 1.1 1995/12/18 03:29:50 paulus Exp $ +# + +#ARCHFLAGS = -arch i386 -arch m68k -arch hppa -arch sparc +#ARCHFLAGS = -arch i386 -arch m68k +#ARCHFLAGS = -arch i386 +ARCHFLAGS = + +BINDIR = /usr/local/ppp/bin +MANDIR = /usr/local/ppp/man +ETCDIR = /etc/ppp +# +# If you change this pathname, you must also change the path +# in and rc.local (or rc.ppp). +# +LKS_DIR=/usr/local/ppp/reloc + +IFLAGS = -I. -I.. -I../pppd + +# +# NUM_PPP=x The number of ppp interfaces you want to create +# VJC If defined, enables VJ header compression +# PPP_COMPRESS If defined, enables BSD packet compression +# HAS_BROKEN_TIOCSPGRP Fixes broken IOCTL with NeXT serial drivers +# ADD_ERRORS If defined adds pseudo-random errors to packets. +# (Only really used for robustness testing.) +# OLD_MUX Fixes double buffer problem with the MuX serial +# driver. +# NBPFILTER If defined, adds hooks for the Berkley Packet Filter +# If this symbol is defined, you must load the +# bpf_reloc LKS _BEFORE_ you load the ppp_reloc LKS. +# See the ./bpf directory for more information. +# +# OPTIMIZE_PPPREND Enable an optimization supported by the NeXT serial +# drivers. Basically they buffer characters for an +# interrupt and call ppprend. + +DFLAGS = -DINET -DKERNEL -DMACH -DNBPFILTER \ + -DNUM_PPP=2 -DVJC -DPPP_COMPRESS \ + -DDEBUG -DHAS_BROKEN_TIOCSPGRP -DOLD_MUX \ + -DOPTIMIZE_PPPREND + + +CFLAGS = -O $(ARCHFLAGS) $(DFLAGS) $(IFLAGS) + +OBJS = bsd-comp.o if_ppp.o ppp_tty.o vjcompress.o random.o +SRCS = $(OBJS:.o=.c) + + +# +# Default target +# +all: ppp_reloc + cd bpf; $(MAKE) ARCHFLAGS="$(ARCHFLAGS)" all + + +install: all + /bin/mkdirs $(ETCDIR) $(LKS_DIR) + install -c -m 644 -o root -g daemon ppp_reloc $(LKS_DIR) + touch $(ETCDIR)/options + cd bpf; $(MAKE) ARCHFLAGS="$(ARCHFLAGS)" install + +ppp_reloc: $(OBJS) Load_Commands.sect Unload_Commands.sect + kl_ld $(ARCHFLAGS) -n ppp -l Load_Commands.sect -u Unload_Commands.sect \ + -i instance -o $@ $(OBJS) + +clean: + rm -f ppp_reloc ppp_loadable core make $(OBJS) *~ #* .depend + cd bpf; $(MAKE) clean + +depend: + cc -M $(CFLAGS) $(SRCS) >.depend + +$(SRCS): if_pppvar.h nbq.h inlines.h + +#.depend: $(SRCS) $(PPPSTATSRCS) +# cc -M $(CFLAGS) $(SRCS) >.depend +# @echo Check dependencies if needed + +FORCE: + +#include .depend diff --git a/NeXT/Makefile.top b/NeXT/Makefile.top new file mode 100644 index 0000000..c281bc3 --- /dev/null +++ b/NeXT/Makefile.top @@ -0,0 +1,199 @@ + +# ppp top level makefile for NeXT systems +# +# $Id: Makefile.top,v 1.1 1995/12/18 03:29:52 paulus Exp $ +# + +BINDIR = /usr/local/ppp/bin +MANDIR = /usr/local/ppp/man +ETCDIR = /etc/ppp +#ARCHFLAGS = -arch i386 -arch m68k -arch hppa -arch sparc +#ARCHFLAGS = -arch i386 -arch m68k +#ARCHFLAGS = -arch i386 +ARCHFLAGS = + +all: + cd chat; $(MAKE) -f Makefile.NeXT ARCHFLAGS="$(ARCHFLAGS)" all + cd pppstats; $(MAKE) -f Makefile.NeXT ARCHFLAGS="$(ARCHFLAGS)" all + cd pppd; $(MAKE) -f Makefile.NeXT ARCHFLAGS="$(ARCHFLAGS)" all + cd NeXT; $(MAKE) ARCHFLAGS="$(ARCHFLAGS)" all + +install: + cd chat; \ + $(MAKE) -f Makefile.NeXT ARCHFLAGS="$(ARCHFLAGS)" BINDIR=$(BINDIR) MANDIR=$(MANDIR) install + cd pppstats; \ + $(MAKE) -f Makefile.NeXT ARCHFLAGS="$(ARCHFLAGS)" BINDIR=$(BINDIR) MANDIR=$(MANDIR) install + cd pppd; \ + $(MAKE) -f Makefile.NeXT ARCHFLAGS="$(ARCHFLAGS)" BINDIR=$(BINDIR) MANDIR=$(MANDIR) install + cd NeXT; \ + $(MAKE) ARCHFLAGS="$(ARCHFLAGS)" BINDIR=$(BINDIR) ETCDIR=$(ETCDIR) install + +clean: + rm -f *~ + (cd chat; $(MAKE) -f Makefile.NeXT clean) + (cd pppstats; $(MAKE) -f Makefile.NeXT clean) + (cd pppd; $(MAKE) -f Makefile.NeXT clean) + (cd NeXT; $(MAKE) clean) + + + +NEXTTOP = \ + README.NeXT \ + ${NULL} + +NEXTPPPD = \ + pppd/Makefile.NeXT \ + pppd/ipcp.c \ + pppd/ipcp.h \ + pppd/options.c \ + pppd/sys-NeXT.c \ + ${NULL} + +NEXTNET = \ + ${NULL} + + +# +# Currently not supported +# +NEXTPPPEXC = \ + pppexclude/Makefile.NeXT \ + pppexclude/pppipexclude.8 \ + pppexclude/pppipexclude.c \ + ${NULL} + +NEXTSTATS = \ + pppstats/Makefile.NeXT \ + ${NULL} + +NEXTCHAT = \ + chat/Makefile.NeXT \ + chat/chat.c.3.2 \ + ${NULL} + +NEXTLKS = \ + NeXT/INSTALL \ + NeXT/Load_Commands.sect \ + NeXT/Makefile \ + NeXT/Makefile.top \ + NeXT/NeXT_Version \ + NeXT/README.NeXT \ + NeXT/TODO.NeXT \ + NeXT/Unload_Commands.sect \ + NeXT/bsd-comp.c \ + NeXT/if_ppp.c \ + NeXT/if_pppvar.h \ + NeXT/inlines.h \ + NeXT/linedisc.h \ + NeXT/nbq.h \ + NeXT/netbuf.h \ + NeXT/ppp_tty.c \ + NeXT/random.c \ + NeXT/random.h \ + NeXT/spl.h \ + NeXT/vjcompress.c \ + ${NULL} + +NEXTHPPA = \ + NeXT/hppa/README.hppa \ + NeXT/hppa/hppaSerialPatch.tar.gz \ + $(NULL} + +NEXTMACH = \ + NeXT/mach/features.h \ + $(NULL} + + +NEXTBPF = \ + NeXT/bpf/Load_Commands.sect \ + NeXT/bpf/Makefile \ + NeXT/bpf/NeXT_Support.c \ + NeXT/bpf/README.NeXT \ + NeXT/bpf/Unload_Commands.sect \ + NeXT/bpf/bpf.4 \ + NeXT/bpf/bpf.c \ + NeXT/bpf/bpf.h \ + NeXT/bpf/bpf_compat.h \ + NeXT/bpf/bpf_filter.c \ + NeXT/bpf/bpfdesc.h \ + NeXT/bpf/netbuf.h \ + NeXT/bpf/promisc.c \ + NeXT/bpf/spl.h \ + ${NULL} + +NEXTEXAMP = \ + Examples/NXHosting_with_PPP \ + Examples/Persistent_Connection \ + Examples/README \ + Examples/flow-control-hints \ + Examples/ip-down.example \ + Examples/ip-up.example \ + Examples/options \ + Examples/pdial \ + Examples/ppp_multiple_hosts.tar.gz \ + Examples/pppdown \ + Examples/pppkill.c \ + Examples/pppup.annex \ + Examples/pppup.direct \ + Examples/pppup.portmaster \ + Examples/pppup.remote \ + Examples/pppup.zyxel \ + Examples/rc.ppp \ + Examples/redial.sh \ + ${NULL} + + +NEXTPORT = \ + ${NEXTTOP} \ + ${NEXTPPPD} \ + ${NEXTSTATS} \ + ${NEXTCHAT} \ + ${NEXTLKS} \ + ${NEXTHPPA} \ + ${NEXTMACH} \ + ${NEXTBPF} \ + ${NEXTEXAMP} \ + ${NULL} + + + +# +# The idea is to make for easy distribution of new port material. +# Just make sure all relevant files appear in the lists above, +# then: +# 1) Update the file ./NeXT_Version with a new number +# 2) go to the top level directory and type 'make portdist'. +# You should get a nice tared/compressed file that is ready for +# the archive. +# + + +portdist: + echo NeXT-ppp2.2-`sed -e '/version_string/!d' \ + -e 's/[^0-9.]*\([0-9.]*\).*/\1/' -e q ./NeXT/NeXT_Version` > .fname + rm -rf `cat .fname` + mkdir `cat .fname` + mkdir `cat .fname`/pppd + mkdir `cat .fname`/chat + mkdir `cat .fname`/NeXT + mkdir `cat .fname`/NeXT/hppa + mkdir `cat .fname`/NeXT/mach + mkdir `cat .fname`/NeXT/bpf + mkdir `cat .fname`/pppstats + mkdir `cat .fname`/Examples + -ln $(NEXTTOP) `cat .fname` + -ln $(NEXTPPPD) `cat .fname`/pppd + -ln $(NEXTCHAT) `cat .fname`/chat + -ln $(NEXTLKS) `cat .fname`/NeXT + -ln $(NEXTHPPA) `cat .fname`/NeXT/hppa + -ln $(NEXTMACH) `cat .fname`/NeXT/mach + -ln $(NEXTBPF) `cat .fname`/NeXT/bpf + -ln $(NEXTSTATS) `cat .fname`/pppstats + -ln $(NEXTEXAMP) `cat .fname`/Examples + for file in $(NEXTPORT); do \ + test -r `cat .fname`/$$file || cp -p $$file `cat .fname`; \ + done + (cd `cat .fname`;tar chf - '.' | gzip --best > ../`cat ../.fname`.tar.gz) + rm -rf `cat .fname` .fname + +.PHONY: dialppp portdist diff --git a/NeXT/NeXT_Version b/NeXT/NeXT_Version new file mode 100644 index 0000000..a137f36 --- /dev/null +++ b/NeXT/NeXT_Version @@ -0,0 +1,6 @@ +# +# ppp-2.2 port to the NeXT +# version_string 0.4.6 +# +# $Id: NeXT_Version,v 1.1 1995/12/18 03:29:53 paulus Exp $ +# diff --git a/NeXT/README.NeXT b/NeXT/README.NeXT new file mode 100644 index 0000000..0f0a6ab --- /dev/null +++ b/NeXT/README.NeXT @@ -0,0 +1,168 @@ +# +# $Id: README.NeXT,v 1.1 1995/12/18 03:29:54 paulus Exp $ +# + +This file has been "reset" for ppp-2.2. + +---------------------------------------------------------------------- + +The NeXT OS does not provide a way of determining the ethernet address +of a particular interface and thus the -proxyarp option to pppd and +this code does not work in quite the way that it would on other +versions. Given an ethernet interface, to find the address for the +code will firstly find the hostname associated with that interface's +IP address. From here it tries 2 methods of obtaining the hardware +address. The first is to look for an entry in the ethernets +database. This can be done by simplay placing a single line in +/etc/ethers with your address and hostname. The second method used is +to look for the ethernet address in the / netinfo domain. This will +most likely have the ethernet address on systems where a configuration +server is used to boot the client machines. Thus on a large numbver of +systems the code will find the ethernet address without any +modification of /etc/ethers. If for any reason the address as given by +netinfo needs to be overridden then this can be done by placing an +ethry in /etc/ethers which is then used in preference. This code has +been tested on both black and white hardware where it does seem to +work correctly. + +Thanks to Pete French for the code fix. + +Status: installed fix. Proxy-arp works as stated above. + +---------------------------------------------------------------------- + +The ioctl problems for NS Intel have been reduced. :) A real workaround for +the errors in PPPIOCG* routines is in place. The ioctl macro is only +used to handle the bad return values. See ppptioctl() for a +description of the fix. + +This fix also fixed the problems encountered when trying to use +multiple interfaces. Previously, the second interface would steal +from the first. + +I have been in contact with NeXT about this bug. I hope that it +will be fixed in 3.3. It turns out, after further study, that the +problem only occurred when using the NeXT supplied serial drivers. +The MuX driver worked flawlessly (thanks Mark!). However, please note +that PPP works with either driver installed. + +---------------------------------------------------------------------- + +============== +From ppp-2.1.2 +============== + +# +# LKS for NeXT OS +# +# $ORIG: README.NeXT,v 1.2 1994/10/02 19:34:44 perkins Exp $ +# + +Known Problems: The following are excerpts from mail messages + (sometimes concatenations from several people or + edited). + +---------------------------------------------------------------------- + +for NS intel: + +I wasn't able to get LKSs working with ppp_reloc placed in +/etc/kern_loader.conf at all. rc.net insisted on setting the +interface flags to UP, and I wasn't able to get it not to by changing +iftab the way I could on black hardware. + +Rather than wasting time trying to debug rc.net, I just took ppp_reloc +out of kern_loader.conf and used kl_util from rc.local to load ppp +(which happens after netinfo is up and running, and after rc.net). + +If you insist on installing your LKS in /usr/lib/kern_loader/* and +modifying your /etc/kern_loader.conf appropriately, you will want to +add a line like: + +ppp* !false + +in /etc/iftab. Again, note that this approach does not work correctly +on Intel based systems. The suggested approach is to modify +/etc/rc.local as suggested in the file INSTALL. + +Status: Work around by calling rc.ppp from rc.local + +----------------------------------------------------------------------- + +It seems that some modems, specifically my telebit T3000 take a little +bit of time to initialize after a DTR drop, so if "modem" is set on +the command line, they can [accidentally] drop the first part of the +chat command. An easy fix is to put delay into the script, or just +change the code in main.c (pppd) to: + + if (!default_device && modem) { + setdtr(fd, FALSE); + sleep(1); + setdtr(fd, TRUE); + sleep(2); /* <-- Give modems time to reinit after DTR drop*/ + +Also, I am among the many having difficulty getting a SIGHUP when the +peer drops the connection (on the dial out case). + +status: Decided for the time being that this should be added to + the chat script by using the \\d construct. + + The SIGHUP problem is addressed in the next release. + A temporary fix is to use the '/bin/csh' instead of the + normal '/bin/sh' in the script that starts pppd. + +---------------------------------------------------------------------- +For Proxy-arp, there is a problem in not finding the ether address +correctly. The address is marked as AF_UNSPEC and full of zeroes +rather than being AF_DLI. I don't know quite why as yet. + +The bug also occurs under 3.0 systems too. Has anyone at NeXT +commented on this bug? + +NeXT does not provide a way of determining the ethernet address of a +particular interface and thus this code does not work in quite the way +that it would on other versions. Given an ethernet interface, to find +the address for the code will firstly find the hostname associated +with that interface's IP address. From here it tries 2 methods of +obtaining the hardware address. The first is to look for an entry in +the ethernets database. This can be done by simplay placing a single +line in /etc/ethers with your address and hostname. The second method +used is to look for the ethernet address in the / netinfo domain. This +will most likely have the ethernet address on systems where a +configuration server is used to boot the client machines. Thus on a +large numbver of systems the code will find the ethernet address +without any modification of /etc/ethers. If for any reason the address +as given by netinfo needs to be overridden then this can be done by +placing an ethry in /etc/ethers which is then used in preference. This +code has been tested on both black and white hardware where it does +seem to work correctly. + +Thanks to Pete French for the code fix. + +Status: installed fix. Proxy-arp works as stated above. + +---------------------------------------------------------------------- + +This kernel is known to work on black and white hardware runnign OS +GG3.2. On White hareware if you should run the NeXT supplied serial drivers. +It currently does not work with the MuX driver. However, we are in +contact with the MuX developers and are working on a solution. + + Rumors abound that MuX v1.4 works as long as you undefine + `DOUBLE_BUFFER'. + +This has not been thoroughly verified. + +---------------------------------------------------------------------- + +If you change the LKS_DIR installation directory to something other +than /usr/lib/kern_loader/ppp, you will probably want to change the +default location that pppstats searches. Do a search for *system and +change the pathname appropriately. + +---------------------------------------------------------------------- + +For problems, send mail to Steve Perkins (perkins@cps.msu.edu). +Please include your hardware type the LKS version number in all +reports. This number may be found in the file /usr/adm/messages (once +the LKS has been installed). diff --git a/NeXT/TODO.NeXT b/NeXT/TODO.NeXT new file mode 100644 index 0000000..20c7780 --- /dev/null +++ b/NeXT/TODO.NeXT @@ -0,0 +1,42 @@ +# +# $Id: TODO.NeXT,v 1.1 1995/12/18 03:29:55 paulus Exp $ +# + +* FAQ on IPCP failure because of AUTH + +* FAQ on shell chat scripts and special characters + +* Add /dev/bpf* in installation docs + +* Check persist option + +* Fix installer to recognize other installation. + +* Add threads to LKS. + +* NUM_PPP in pppd/pppd.h and Makefile are different + +* Unify NBPFILTER symbol + +* Better Load support for BPF. Figure out how to support + NeXT (moto) with driverkit stuff. + +* Verify 1500 minimum + +* Limit resources for runaway UDP. + +* Better support for getting/releasing netbufs. + +* >Jan 10 21:41:46 beatty pppd[1427]: rcvd [IPCP ConfNak id=0x1 ] + +* Break up installer into Smaller packages + +* Fix with VirtModem + +* If the dialing program fails to get a line, but returns success, + then ppp will report that it could not determine the ip address, + and not notice the "received own echo reply" condition. + +* Look into stack overflow as cause of kernel panics. + +* Use kget inside interrupt handler. diff --git a/NeXT/Unload_Commands.sect b/NeXT/Unload_Commands.sect new file mode 100755 index 0000000..81ff88e --- /dev/null +++ b/NeXT/Unload_Commands.sect @@ -0,0 +1 @@ +CALL pppdetach 0 diff --git a/NeXT/bsd-comp.c b/NeXT/bsd-comp.c new file mode 100644 index 0000000..0e3dd41 --- /dev/null +++ b/NeXT/bsd-comp.c @@ -0,0 +1,1075 @@ +/* Because this code is derived from the 4.3BSD compress source: + * + * + * Copyright (c) 1985, 1986 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * James A. Woods, derived from original work by Spencer Thomas + * and Joseph Orost. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * Rewritten for NextStep's funky kernel functions, I/O threads, + * and netbufs (instead of real mbufs). Also, ifnets don't install + * into the kernel under NS as they do under BSD. We have tried to + * make the code remain as similar to the NetBSD version without + * incurring too much hassle. This code is the merge of + * Philip Prindeville's /Pete French's + * and Stephen Perkins' independent ports. + * + */ + +/* + * This version is for use with mbufs on BSD-derived systems. + * + * $Id: bsd-comp.c,v 1.1 1995/12/18 03:30:00 paulus Exp $ + */ + +#include +#include +#include + +#define KERNEL 1 +#include +#include + +#include +#include + +#define PACKETPTR netbuf_t +#include + +#include "nbq.h" + +/* + * We align with this number of bits zero. The code makes the somewhat + * suspect assumption that an address can be held in an unsigned long. + * Sadly this is necessary to do bit operations on it. + */ + +#define Z_ALIGN 3 /* 8 byte boudary */ +#define Z_EXTRA ((unsigned long)((1< +#include +#include +#include + +#define mtod(m,type) ((type)nb_map(m)) +typedef unsigned short u_int16_t; + +/* + * PPP "BSD compress" compression + * The differences between this compression and the classic BSD LZW + * source are obvious from the requirement that the classic code worked + * with files while this handles arbitrarily long streams that + * are broken into packets. They are: + * + * When the code size expands, a block of junk is not emitted by + * the compressor and not expected by the decompressor. + * + * New codes are not necessarily assigned every time an old + * code is output by the compressor. This is because a packet + * end forces a code to be emitted, but does not imply that a + * new sequence has been seen. + * + * The compression ratio is checked at the first end of a packet + * after the appropriate gap. Besides simplifying and speeding + * things up, this makes it more likely that the transmitter + * and receiver will agree when the dictionary is cleared when + * compression is not going well. + */ + +/* + * A dictionary for doing BSD compress. + */ +struct bsd_db { + void *kbase; /* actual kalloc'd address for struct */ + void *klens; /* actual kalloc'd address for lens */ + int totlen; /* length of this structure */ + u_int hsize; /* size of the hash table */ + u_char hshift; /* used in hash function */ + u_char n_bits; /* current bits/code */ + u_char maxbits; + u_char debug; + u_char unit; + u_int16_t seqno; /* sequence # of next packet */ + u_int hdrlen; /* header length to preallocate */ + u_int mru; + u_int maxmaxcode; /* largest valid code */ + u_int max_ent; /* largest code in use */ + u_int in_count; /* uncompressed bytes, aged */ + u_int bytes_out; /* compressed bytes, aged */ + u_int ratio; /* recent compression ratio */ + u_int checkpoint; /* when to next check the ratio */ + u_int clear_count; /* times dictionary cleared */ + u_int incomp_count; /* incompressible packets */ + u_int incomp_bytes; /* incompressible bytes */ + u_int uncomp_count; /* uncompressed packets */ + u_int uncomp_bytes; /* uncompressed bytes */ + u_int comp_count; /* compressed packets */ + u_int comp_bytes; /* compressed bytes */ + u_int16_t *lens; /* array of lengths of codes */ + struct bsd_dict { + union { /* hash value */ + u_int32_t fcode; + struct { +#if BYTE_ORDER == LITTLE_ENDIAN + u_int16_t prefix; /* preceding code */ + u_char suffix; /* last character of new code */ + u_char pad; +#else + u_char pad; + u_char suffix; /* last character of new code */ + u_int16_t prefix; /* preceding code */ +#endif + } hs; + } f; + u_int16_t codem1; /* output of hash table -1 */ + u_int16_t cptr; /* map code to hash table entry */ + } dict[1]; +}; + +#define BSD_OVHD 2 /* BSD compress overhead/packet */ +#define BSD_INIT_BITS BSD_MIN_BITS + +static void *bsd_comp_alloc __P((u_char *options, int opt_len)); +static void *bsd_decomp_alloc __P((u_char *options, int opt_len)); +static void bsd_free __P((void *state)); +static int bsd_comp_init __P((void *state, u_char *options, int opt_len, + int unit, int hdrlen, int debug)); +static int bsd_decomp_init __P((void *state, u_char *options, int opt_len, + int unit, int hdrlen, int mru, int debug)); +static int bsd_compress __P((void *state, netbuf_t *mret, + netbuf_t mp, int slen, int maxolen)); +static void bsd_incomp __P((void *state, netbuf_t dmsg)); +static int bsd_decompress __P((void *state, netbuf_t cmp, netbuf_t *dmpp)); +static void bsd_reset __P((void *state)); +static void bsd_comp_stats __P((void *state, struct compstat *stats)); + +/* + * Procedures exported to if_ppp.c. + */ +struct compressor ppp_bsd_compress = { + CI_BSD_COMPRESS, /* compress_proto */ + bsd_comp_alloc, /* comp_alloc */ + bsd_free, /* comp_free */ + bsd_comp_init, /* comp_init */ + bsd_reset, /* comp_reset */ + bsd_compress, /* compress */ + bsd_comp_stats, /* comp_stat */ + bsd_decomp_alloc, /* decomp_alloc */ + bsd_free, /* decomp_free */ + bsd_decomp_init, /* decomp_init */ + bsd_reset, /* decomp_reset */ + bsd_decompress, /* decompress */ + bsd_incomp, /* incomp */ + bsd_comp_stats, /* decomp_stat */ +}; + +/* + * the next two codes should not be changed lightly, as they must not + * lie within the contiguous general code space. + */ +#define CLEAR 256 /* table clear output code */ +#define FIRST 257 /* first free entry */ +#define LAST 255 + +#define MAXCODE(b) ((1 << (b)) - 1) +#define BADCODEM1 MAXCODE(BSD_MAX_BITS) + +#define BSD_HASH(prefix,suffix,hshift) ((((u_int32_t)(suffix)) << (hshift)) \ + ^ (u_int32_t)(prefix)) +#define BSD_KEY(prefix,suffix) ((((u_int32_t)(suffix)) << 16) \ + + (u_int32_t)(prefix)) + +#define CHECK_GAP 10000 /* Ratio check interval */ + +#define RATIO_SCALE_LOG 8 +#define RATIO_SCALE (1<>RATIO_SCALE_LOG) + +/* Could include inlines.h */ +#ifndef IOLog +#define IOLog printf +#define IOLogDbg if (db->debug) printf +#else +#define IOLogDbg if (db->debug) IOLog +#endif + +/* + * clear the dictionary + */ +static void +bsd_clear(db) + struct bsd_db *db; +{ + db->clear_count++; + db->max_ent = FIRST-1; + db->n_bits = BSD_INIT_BITS; + db->ratio = 0; + db->bytes_out = 0; + db->in_count = 0; + db->incomp_count = 0; + db->checkpoint = CHECK_GAP; +} + +/* + * If the dictionary is full, then see if it is time to reset it. + * + * Compute the compression ratio using fixed-point arithmetic + * with 8 fractional bits. + * + * Since we have an infinite stream instead of a single file, + * watch only the local compression ratio. + * + * Since both peers must reset the dictionary at the same time even in + * the absence of CLEAR codes (while packets are incompressible), they + * must compute the same ratio. + */ +static int /* 1=output CLEAR */ +bsd_check(db) + struct bsd_db *db; +{ + u_int new_ratio; + + if (db->in_count >= db->checkpoint) { + /* age the ratio by limiting the size of the counts */ + if (db->in_count >= RATIO_MAX + || db->bytes_out >= RATIO_MAX) { + db->in_count -= db->in_count/4; + db->bytes_out -= db->bytes_out/4; + } + + db->checkpoint = db->in_count + CHECK_GAP; + + if (db->max_ent >= db->maxmaxcode) { + /* Reset the dictionary only if the ratio is worse, + * or if it looks as if it has been poisoned + * by incompressible data. + * + * This does not overflow, because + * db->in_count <= RATIO_MAX. + */ + new_ratio = db->in_count << RATIO_SCALE_LOG; + if (db->bytes_out != 0) + new_ratio /= db->bytes_out; + + if (new_ratio < db->ratio || new_ratio < 1 * RATIO_SCALE) { + bsd_clear(db); + return 1; + } + db->ratio = new_ratio; + } + } + return 0; +} + +/* + * Return statistics. + */ +static void +bsd_comp_stats(state, stats) + void *state; + struct compstat *stats; +{ + struct bsd_db *db = (struct bsd_db *) state; + u_int out; + + stats->unc_bytes = db->uncomp_bytes; + stats->unc_packets = db->uncomp_count; + stats->comp_bytes = db->comp_bytes; + stats->comp_packets = db->comp_count; + stats->inc_bytes = db->incomp_bytes; + stats->inc_packets = db->incomp_count; + stats->ratio = db->in_count; + out = db->bytes_out; + if (stats->ratio <= 0x7fffff) + stats->ratio <<= 8; + else + out >>= 8; + if (out != 0) + stats->ratio /= out; +} + +/* + * Reset state, as on a CCP ResetReq. + */ +static void +bsd_reset(state) + void *state; +{ + struct bsd_db *db = (struct bsd_db *) state; + + db->seqno = 0; + bsd_clear(db); + db->clear_count = 0; +} + +/* + * Allocate space for a (de) compressor. + */ +static void * +bsd_alloc(options, opt_len, decomp) + u_char *options; + int opt_len, decomp; +{ + int bits; + u_int newlen, hsize, hshift, maxmaxcode; + struct bsd_db *db; + + if (opt_len != CILEN_BSD_COMPRESS || options[0] != CI_BSD_COMPRESS + || options[1] != CILEN_BSD_COMPRESS + || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION) + return NULL; + bits = BSD_NBITS(options[2]); + switch (bits) { + case 9: /* needs 82152 for both directions */ + case 10: /* needs 84144 */ + case 11: /* needs 88240 */ + case 12: /* needs 96432 */ + hsize = 5003; + hshift = 4; + break; + case 13: /* needs 176784 */ + hsize = 9001; + hshift = 5; + break; + case 14: /* needs 353744 */ + hsize = 18013; + hshift = 6; + break; + case 15: /* needs 691440 */ + hsize = 35023; + hshift = 7; + break; + case 16: /* needs 1366160--far too much, */ + /* hsize = 69001; */ /* and 69001 is too big for cptr */ + /* hshift = 8; */ /* in struct bsd_db */ + /* break; */ + default: + return NULL; + } + + maxmaxcode = MAXCODE(bits); + newlen = sizeof(*db) + (hsize-1) * (sizeof(db->dict[0])); + { + unsigned long kret; + kret = (unsigned long) kalloc(Z_EXTRA + newlen); + if (!kret) + return NULL; + db = (struct bsd_db *) ALIGN(kret); + bzero(db, sizeof(*db) - sizeof(db->dict)); + db->kbase = (void *)kret; + } + + if (!decomp) { + db->lens = NULL; + } else { + unsigned long kret; + kret = (unsigned long) kalloc(Z_EXTRA + + ((maxmaxcode+1) * sizeof(db->lens[0]))); + if (!kret) { + kfree(db->kbase, newlen + Z_EXTRA); + return NULL; + } + db->lens = (u_int16_t *) ALIGN(kret); + db->klens = (void *) kret; + } + + db->totlen = newlen; + db->hsize = hsize; + db->hshift = hshift; + db->maxmaxcode = maxmaxcode; + db->maxbits = bits; + + return (void *) db; +} + +static void +bsd_free(state) + void *state; +{ + struct bsd_db *db = (struct bsd_db *) state; + + if (db->lens) + kfree(db->klens, ((db->maxmaxcode+1) * sizeof(db->lens[0])) + Z_EXTRA); + kfree(db->kbase, db->totlen + Z_EXTRA); +} + +static void * +bsd_comp_alloc(options, opt_len) + u_char *options; + int opt_len; +{ + return bsd_alloc(options, opt_len, 0); +} + +static void * +bsd_decomp_alloc(options, opt_len) + u_char *options; + int opt_len; +{ + return bsd_alloc(options, opt_len, 1); +} + +/* + * Initialize the database. + */ +static int +bsd_init(db, options, opt_len, unit, hdrlen, mru, debug, decomp) + struct bsd_db *db; + u_char *options; + int opt_len, unit, hdrlen, mru, debug, decomp; +{ + int i; + + if (opt_len != CILEN_BSD_COMPRESS || options[0] != CI_BSD_COMPRESS + || options[1] != CILEN_BSD_COMPRESS + || BSD_VERSION(options[2]) != BSD_CURRENT_VERSION + || BSD_NBITS(options[2]) != db->maxbits + || decomp && db->lens == NULL) + return 0; + + if (decomp) { + i = LAST+1; + while (i != 0) + db->lens[--i] = 1; + } + i = db->hsize; + while (i != 0) { + db->dict[--i].codem1 = BADCODEM1; + db->dict[i].cptr = 0; + } + + db->unit = unit; + db->hdrlen = hdrlen; + db->mru = mru; +#ifndef DEBUG + if (debug) +#endif + db->debug = 1; + + bsd_reset(db); + + return 1; +} + +static int +bsd_comp_init(state, options, opt_len, unit, hdrlen, debug) + void *state; + u_char *options; + int opt_len, unit, hdrlen, debug; +{ + return bsd_init((struct bsd_db *) state, options, opt_len, + unit, hdrlen, 0, debug, 0); +} + +static int +bsd_decomp_init(state, options, opt_len, unit, hdrlen, mru, debug) + void *state; + u_char *options; + int opt_len, unit, hdrlen, mru, debug; +{ + return bsd_init((struct bsd_db *) state, options, opt_len, + unit, hdrlen, mru, debug, 1); +} + + +/* + * compress a packet + * One change from the BSD compress command is that when the + * code size expands, we do not output a bunch of padding. + */ +int /* new slen */ +bsd_compress(state, mret, mp, slen, maxolen) + void *state; + netbuf_t *mret; /* return compressed netbuf here */ + netbuf_t mp; /* from here */ + int slen; /* uncompressed length */ + int maxolen; /* max compressed length */ +{ + struct bsd_db *db = (struct bsd_db *) state; + int hshift = db->hshift; + u_int max_ent = db->max_ent; + u_int n_bits = db->n_bits; + u_int bitno = 32; + u_int32_t accm = 0, fcode; + struct bsd_dict *dictp; + u_char c; + int hval, disp, ent, ilen; + u_char *rptr, *wptr; + u_char *cp_end; + int olen; + netbuf_t m; + +#define PUTBYTE(v) { \ + ++olen; \ + if (wptr) { \ + *wptr++ = (v); \ + if (wptr >= cp_end) \ + wptr = NULL; \ + } \ +} + +#define OUTPUT(ent) { \ + bitno -= n_bits; \ + accm |= ((ent) << bitno); \ + do { \ + PUTBYTE(accm >> 24); \ + accm <<= 8; \ + bitno += 8; \ + } while (bitno <= 24); \ +} + + /* + * If the protocol is not in the range we're interested in, + * just return without compressing the packet. If it is, + * the protocol becomes the first byte to compress. + */ + rptr = mtod(mp, u_char *); + ent = PPP_PROTOCOL(rptr); + if (ent < CI_BSD_COMPRESS || ent > 0xf9) { + *mret = NULL; + return slen; + } + + /* Don't generate compressed packets which are larger than + the uncompressed packet. */ + if (maxolen > slen) + maxolen = slen; + + /* Allocate one mbuf to start with. (don't forget space for the FCS!) */ + m = ppp_nb_alloc(maxolen + db->hdrlen + PPP_FCSLEN); + *mret = m; + if (m != NULL) { + if (db->hdrlen > 0) + ppp_nb_shrink_top(m, db->hdrlen); + nb_shrink_bot(m, PPP_FCSLEN); /* grown by pppstart() */ + wptr = mtod(m, u_char *); + cp_end = wptr + maxolen; + } else + wptr = cp_end = NULL; + + /* + * Copy the PPP header over, changing the protocol, + * and install the 2-byte packet sequence number. + */ + if (wptr) { + *wptr++ = PPP_ADDRESS(rptr); /* assumes the ppp header is */ + *wptr++ = PPP_CONTROL(rptr); /* all in one mbuf */ + *wptr++ = 0; /* change the protocol */ + *wptr++ = PPP_COMP; + *wptr++ = db->seqno >> 8; + *wptr++ = db->seqno; + } + ++db->seqno; + + olen = 0; + rptr += PPP_HDRLEN; + slen = nb_size(mp) - PPP_HDRLEN; + ilen = slen + 1; + while (slen > 0) { + slen--; + c = *rptr++; + fcode = BSD_KEY(ent, c); + hval = BSD_HASH(ent, c, hshift); + dictp = &db->dict[hval]; + + /* Validate and then check the entry. */ + if (dictp->codem1 >= max_ent) + goto nomatch; + if (dictp->f.fcode == fcode) { + ent = dictp->codem1+1; + continue; /* found (prefix,suffix) */ + } + + /* continue probing until a match or invalid entry */ + disp = (hval == 0) ? 1 : hval; + do { + hval += disp; + if (hval >= db->hsize) + hval -= db->hsize; + dictp = &db->dict[hval]; + if (dictp->codem1 >= max_ent) + goto nomatch; + } while (dictp->f.fcode != fcode); + ent = dictp->codem1 + 1; /* finally found (prefix,suffix) */ + continue; + + nomatch: + OUTPUT(ent); /* output the prefix */ + + /* code -> hashtable */ + if (max_ent < db->maxmaxcode) { + struct bsd_dict *dictp2; + /* expand code size if needed */ + if (max_ent >= MAXCODE(n_bits)) + db->n_bits = ++n_bits; + + /* Invalidate old hash table entry using + * this code, and then take it over. + */ + dictp2 = &db->dict[max_ent+1]; + if (db->dict[dictp2->cptr].codem1 == max_ent) + db->dict[dictp2->cptr].codem1 = BADCODEM1; + dictp2->cptr = hval; + dictp->codem1 = max_ent; + dictp->f.fcode = fcode; + + db->max_ent = ++max_ent; + } + ent = c; + } + + OUTPUT(ent); /* output the last code */ + db->bytes_out += olen; + db->in_count += ilen; + if (bitno < 32) + ++db->bytes_out; /* count complete bytes */ + + if (bsd_check(db)) + OUTPUT(CLEAR); /* do not count the CLEAR */ + + /* + * Pad dribble bits of last code with ones. + * Do not emit a completely useless byte of ones. + */ + if (bitno != 32) + PUTBYTE((accm | (0xff << (bitno-8))) >> 24); + + /* + * Increase code size if we would have without the packet + * boundary and as the decompressor will. + */ + if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) + db->n_bits++; + + db->uncomp_bytes += ilen; + ++db->uncomp_count; + if (olen + PPP_HDRLEN + BSD_OVHD > maxolen || wptr == NULL) { + /* throw away the compressed stuff if it is longer than uncompressed */ + if (*mret != NULL) { + nb_free(*mret); + *mret = NULL; + } + ++db->incomp_count; + db->incomp_bytes += ilen; + } else { + nb_shrink_bot(m, nb_size(m) - (wptr - mtod(m, u_char *))); + ++db->comp_count; + db->comp_bytes += olen + BSD_OVHD; + } + + return olen + PPP_HDRLEN + BSD_OVHD; +#undef OUTPUT +#undef PUTBYTE +} + + +/* + * Update the "BSD Compress" dictionary on the receiver for + * incompressible data by pretending to compress the incoming data. + */ +static void +bsd_incomp(state, dmsg) + void *state; + netbuf_t dmsg; +{ + struct bsd_db *db = (struct bsd_db *) state; + u_int hshift = db->hshift; + u_int max_ent = db->max_ent; + u_int n_bits = db->n_bits; + struct bsd_dict *dictp; + u_int32_t fcode; + u_char c; + u_int32_t hval, disp; + int slen, ilen; + u_int bitno = 7; + u_char *rptr; + u_int ent; + + /* + * If the protocol is not in the range we're interested in, + * just return without looking at the packet. If it is, + * the protocol becomes the first byte to "compress". + */ + rptr = mtod(dmsg, u_char *); + ent = PPP_PROTOCOL(rptr); + if (ent < CI_BSD_COMPRESS || ent > 0xf9) + return; + + db->incomp_count++; + db->seqno++; + ilen = 1; /* count the protocol as 1 byte */ + rptr += PPP_HDRLEN; + slen = nb_size(dmsg) - PPP_HDRLEN; + ilen += slen; + + do { + c = *rptr++; + fcode = BSD_KEY(ent, c); + hval = BSD_HASH(ent, c, hshift); + dictp = &db->dict[hval]; + + /* validate and then check the entry */ + if (dictp->codem1 >= max_ent) + goto nomatch; + if (dictp->f.fcode == fcode) { + ent = dictp->codem1+1; + continue; /* found (prefix,suffix) */ + } + + /* continue probing until a match or invalid entry */ + disp = (hval == 0) ? 1 : hval; + do { + hval += disp; + if (hval >= db->hsize) + hval -= db->hsize; + dictp = &db->dict[hval]; + if (dictp->codem1 >= max_ent) + goto nomatch; + } while (dictp->f.fcode != fcode); + ent = dictp->codem1+1; + continue; /* finally found (prefix,suffix) */ + + nomatch: /* output (count) the prefix */ + bitno += n_bits; + + /* code -> hashtable */ + if (max_ent < db->maxmaxcode) { + struct bsd_dict *dictp2; + /* expand code size if needed */ + if (max_ent >= MAXCODE(n_bits)) + db->n_bits = ++n_bits; + + /* Invalidate previous hash table entry + * assigned this code, and then take it over. + */ + dictp2 = &db->dict[max_ent+1]; + if (db->dict[dictp2->cptr].codem1 == max_ent) + db->dict[dictp2->cptr].codem1 = BADCODEM1; + dictp2->cptr = hval; + dictp->codem1 = max_ent; + dictp->f.fcode = fcode; + + db->max_ent = ++max_ent; + db->lens[max_ent] = db->lens[ent]+1; + } + ent = c; + } while (--slen != 0); + bitno += n_bits; /* output (count) the last code */ + db->bytes_out += bitno/8; + db->in_count += ilen; + (void)bsd_check(db); + + ++db->incomp_count; + db->incomp_bytes += ilen; + ++db->uncomp_count; + db->uncomp_bytes += ilen; + + /* Increase code size if we would have without the packet + * boundary and as the decompressor will. + */ + if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) + db->n_bits++; +} + + +/* + * Decompress "BSD Compress". + * + * Because of patent problems, we return DECOMP_ERROR for errors + * found by inspecting the input data and for system problems, but + * DECOMP_FATALERROR for any errors which could possibly be said to + * be being detected "after" decompression. For DECOMP_ERROR, + * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be + * infringing a patent of Motorola's if we do, so we take CCP down + * instead. + * + * Given that the frame has the correct sequence number and a good FCS, + * errors such as invalid codes in the input most likely indicate a + * bug, so we return DECOMP_FATALERROR for them in order to turn off + * compression, even though they are detected by inspecting the input. + */ +int +bsd_decompress(state, cmp, dmpp) + void *state; + netbuf_t cmp, *dmpp; +{ + struct bsd_db *db = (struct bsd_db *) state; + u_int max_ent = db->max_ent; + u_int32_t accm = 0; + u_int bitno = 32; /* 1st valid bit in accm */ + u_int n_bits = db->n_bits; + u_int tgtbitno = 32-n_bits; /* bitno when we have a code */ + struct bsd_dict *dictp; + int explen, seq, len; + u_int incode, oldcode, finchar; + u_char *p, *rptr, *wptr; + netbuf_t dmp, mret; + int adrs, ctrl, ilen; + int space, codelen, extra, maxilen; + + /* + * Save the address/control from the PPP header + * and then get the sequence number. + */ + *dmpp = NULL; + rptr = mtod(cmp, u_char *); + adrs = PPP_ADDRESS(rptr); + ctrl = PPP_CONTROL(rptr); + rptr += PPP_HDRLEN; + len = nb_size(cmp) - PPP_HDRLEN; + seq = (rptr[0] << 8) + rptr[1]; + rptr += BSD_OVHD; + len -= BSD_OVHD; + + /* + * Check the sequence number and give up if it differs from + * the value we're expecting. + */ + if (seq != db->seqno) { + IOLogDbg("bsd_decomp%d: bad sequence # %d, expected %d\n", + db->unit, seq, db->seqno - 1); + return DECOMP_ERROR; + } + ++db->seqno; + + /* + * Allocate an netbuf large enough for all the data. + */ + maxilen = db->mru + db->hdrlen + PPP_HDRLEN; /* no FCS */ + dmp = ppp_nb_alloc(maxilen); /* XXX */ + if (dmp == NULL) + return DECOMP_ERROR; + if (db->hdrlen > 0) + ppp_nb_shrink_top(dmp, db->hdrlen); + mret = dmp; + wptr = mtod(dmp, u_char *); + space = nb_size(dmp) - PPP_HDRLEN + 1; + + /* + * Fill in the ppp header, but not the last byte of the protocol + * (that comes from the decompressed data). + */ + wptr[0] = adrs; + wptr[1] = ctrl; + wptr[2] = 0; + wptr += PPP_HDRLEN - 1; + + ilen = len; + oldcode = CLEAR; + explen = 0; + while (len > 0) { + /* + * Accumulate bytes until we have a complete code. + * Then get the next code, relying on the 32-bit, + * unsigned accm to mask the result. + */ + bitno -= 8; + accm |= *rptr++ << bitno; + --len; + if (tgtbitno < bitno) + continue; + incode = accm >> tgtbitno; + accm <<= n_bits; + bitno += n_bits; + + if (incode == CLEAR) { + /* + * The dictionary must only be cleared at + * the end of a packet. But there could be an + * empty mbuf at the end. + */ + if (len > 0) { + nb_free(mret); + IOLogDbg("bsd_decomp%d: bad CLEAR\n", db->unit); + return DECOMP_FATALERROR; /* probably a bug */ + } + bsd_clear(db); + explen = ilen = 0; + break; + } + + if (incode > max_ent + 2 || incode > db->maxmaxcode + || incode > max_ent && oldcode == CLEAR) { + nb_free(mret); + IOLogDbg("bsd_decomp%d: bad code 0x%x oldcode=0x%x max_ent=0x%x explen=%d seqno=%d\n", + db->unit, incode, oldcode, max_ent, explen, db->seqno); + return DECOMP_FATALERROR; /* probably a bug */ + } + + /* Special case for KwKwK string. */ + if (incode > max_ent) { + finchar = oldcode; + extra = 1; + } else { + finchar = incode; + extra = 0; + } + + codelen = db->lens[finchar]; + explen += codelen + extra; + if (explen > db->mru + 1) { + nb_free(mret); + IOLogDbg("bsd_decomp%d: ran out of mru\n len=%d, finchar=0x%x, codelen=%d, explen=%d\n", + db->unit, len, finchar, codelen, explen); + return DECOMP_FATALERROR; + } + + /* + * If we have no space left, then we've overflowed... + */ + if ((space -= codelen + extra) < 0) { + IOLog("bsd_decompress%d: no space left in netbuf (need %d bytes)\n", + db->unit, (codelen + extra) - space); + nb_free(mret); + return DECOMP_ERROR; + } + + /* + * Decode this code and install it in the decompressed buffer. + */ + p = (wptr += codelen); + while (finchar > LAST) { + dictp = &db->dict[db->dict[finchar].cptr]; +#ifdef DEBUG + if (--codelen <= 0 || dictp->codem1 != finchar-1) + goto bad; +#endif + *--p = dictp->f.hs.suffix; + finchar = dictp->f.hs.prefix; + } + *--p = finchar; + +#ifdef DEBUG + if (--codelen != 0) + IOLog("bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n", + db->unit, codelen, incode, max_ent); +#endif + + if (extra) /* the KwKwK case again */ + *wptr++ = finchar; + + /* + * If not first code in a packet, and + * if not out of code space, then allocate a new code. + * + * Keep the hash table correct so it can be used + * with uncompressed packets. + */ + if (oldcode != CLEAR && max_ent < db->maxmaxcode) { + struct bsd_dict *dictp2; + u_int32_t fcode; + u_int32_t hval, disp; + + fcode = BSD_KEY(oldcode,finchar); + hval = BSD_HASH(oldcode,finchar,db->hshift); + dictp = &db->dict[hval]; + + /* look for a free hash table entry */ + if (dictp->codem1 < max_ent) { + disp = (hval == 0) ? 1 : hval; + do { + hval += disp; + if (hval >= db->hsize) + hval -= db->hsize; + dictp = &db->dict[hval]; + } while (dictp->codem1 < max_ent); + } + + /* + * Invalidate previous hash table entry + * assigned this code, and then take it over + */ + dictp2 = &db->dict[max_ent+1]; + if (db->dict[dictp2->cptr].codem1 == max_ent) { + db->dict[dictp2->cptr].codem1 = BADCODEM1; + } + dictp2->cptr = hval; + dictp->codem1 = max_ent; + dictp->f.fcode = fcode; + + db->max_ent = ++max_ent; + db->lens[max_ent] = db->lens[oldcode]+1; + + /* Expand code size if needed. */ + if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) { + db->n_bits = ++n_bits; + tgtbitno = 32-n_bits; + } + } + oldcode = incode; + } + nb_shrink_bot(dmp, nb_size(dmp) - (wptr - mtod(dmp, u_char *))); + + /* + * Keep the checkpoint right so that incompressible packets + * clear the dictionary at the right times. + */ + db->bytes_out += ilen; + db->in_count += explen; + if (bsd_check(db)) { + IOLogDbg("bsd_decomp%d: peer should have cleared dictionary\n", + db->unit); + } + + ++db->comp_count; + db->comp_bytes += ilen + BSD_OVHD; + ++db->uncomp_count; + db->uncomp_bytes += explen; + + *dmpp = mret; + return DECOMP_OK; + +#ifdef DEBUG + bad: + if (codelen <= 0) { + IOLog("bsd_decomp%d: fell off end of chain 0x%x at 0x%x by 0x%x, max_ent=0x%x\n", + db->unit, incode, finchar, db->dict[finchar].cptr, max_ent); + } else if (dictp->codem1 != finchar-1) { + IOLog("bsd_decomp%d: bad code chain 0x%x finchar=0x%x oldcode=0x%x cptr=0x%x codem1=0x%x\n", + db->unit, incode, finchar, oldcode, db->dict[finchar].cptr, + dictp->codem1); + } + nb_free(mret); + return DECOMP_FATALERROR; +#endif /* DEBUG */ +} +#endif /* DO_BSD_COMPRESS */ diff --git a/NeXT/if_ppp.c b/NeXT/if_ppp.c new file mode 100644 index 0000000..54cabda --- /dev/null +++ b/NeXT/if_ppp.c @@ -0,0 +1,1633 @@ +/* + * if_ppp.c - Point-to-Point Protocol (PPP) Asynchronous driver. + * + * Copyright (c) 1989 Carnegie Mellon University. + * All rights reserved. + * + * Redistribution and use in source and binary forms are permitted + * provided that the above copyright notice and this paragraph are + * duplicated in all such forms and that any documentation, + * advertising materials, and other materials related to such + * distribution and use acknowledge that the software was developed + * by Carnegie Mellon University. The name of the + * University may not be used to endorse or promote products derived + * from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED + * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. + * + * Drew D. Perkins + * Carnegie Mellon University + * 4910 Forbes Ave. + * Pittsburgh, PA 15213 + * (412) 268-8576 + * ddp@andrew.cmu.edu + * + * Based on: + * @(#)if_sl.c 7.6.1.2 (Berkeley) 2/15/89 + * + * Copyright (c) 1987 Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms are permitted + * provided that the above copyright notice and this paragraph are + * duplicated in all such forms and that any documentation, + * advertising materials, and other materials related to such + * distribution and use acknowledge that the software was developed + * by the University of California, Berkeley. The name of the + * University may not be used to endorse or promote products derived + * from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED + * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. + * + * Serial Line interface + * + * Rick Adams + * Center for Seismic Studies + * 1300 N 17th Street, Suite 1450 + * Arlington, Virginia 22209 + * (703)276-7900 + * rick@seismo.ARPA + * seismo!rick + * + * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris). + * Converted to 4.3BSD Beta by Chris Torek. + * Other changes made at Berkeley, based in part on code by Kirk Smith. + * + * Converted to 4.3BSD+ 386BSD by Brad Parker (brad@cayman.com) + * Added VJ tcp header compression; more unified ioctls + * + * Extensively modified by Paul Mackerras (paulus@cs.anu.edu.au). + * Cleaned up a lot of the mbuf-related code to fix bugs that + * caused system crashes and packet corruption. Changed pppstart + * so that it doesn't just give up with a collision if the whole + * packet doesn't fit in the output ring buffer. + * + * Added priority queueing for interactive IP packets, following + * the model of if_sl.c, plus hooks for bpf. + * Paul Mackerras (paulus@cs.anu.edu.au). + * + * Rewritten for NextStep's funky kernel functions, I/O threads, + * and netbufs (instead of real mbufs). Also, ifnets don't install + * into the kernel under NS as they do under BSD. We have tried to + * make the code remain as similar to the NetBSD version without + * incurring too much hassle. This code is the merge of + * Philip Prindeville's /Pete French's + * and Stephen Perkins' independent ports. + * + */ + +/* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */ + +#if !defined(lint) +static char sccsid[] = "$Revision: 1.1 $ ($Date: 1995/12/18 03:30:02 $)"; +#endif /* not lint*/ + +#define KERNEL 1 +#define KERNEL_FEATURES 1 +#define INET 1 + +/* #include "ppp.h" */ + +#include +#include +#include "netbuf.h" +#include +#include +#include +#include +#include +#include +#if defined(m68k) +#import "spl.h" +#else +#include +#import +#endif +#if defined(sparc) || defined(m68k) +#include +#endif +#include + +#include +#include + +#if INET +#include +#include +#include +#include +#endif + + +#include +#ifdef VJC +#include +#endif +#include +#include "if_pppvar.h" + +struct ppp_softc ppp_softc[NUM_PPP]; + +#ifdef PPP_COMPRESS +#define PACKETPTR netbuf_t +#include +#endif + +#ifdef NBPFILTER +#include +/* + * We store the address of necessary BPF functions + * here. + */ +struct bpf_fns fnarg; +#endif + +/* + * The max number of netbuf_ts we wish to compress and cache for + * sending. + */ +#define COMPRESS_CACHE_LEN 5 + +#include "inlines.h" + +/* + * Necessary to avoid redefinition warnings or bogus type casts later. + */ +int pppoutput __P((netif_t ifp, netbuf_t m, void *arg)); +int pppsioctl __P((netif_t ifp, int cmd, caddr_t data)); +int pppcontrol __P((netif_t ifp, const char *cmd, void *data)); +void pppintr_comp __P((void *arg)); +void pppintr_decomp __P((void *arg)); +void pppfillfreeq __P((void *arg)); +void pppgetm __P((register struct ppp_softc *sc)); + +static void ppp_requeue __P((struct ppp_softc *)); +static void ppp_outpkt __P((struct ppp_softc *)); +static void ppp_ccp __P((struct ppp_softc *, netbuf_t, int rcvd)); +static void ppp_ccp_closed __P((struct ppp_softc *)); +static void ppp_inproc __P((struct ppp_softc *, netbuf_t)); +static void pppdumpm __P((netbuf_t)); + +extern int install_ppp_ld __P((void)); +extern int tty_ld_remove __P((int)); + + +/* + * We steal two bits in the mbuf m_flags, to mark high-priority packets + * for output, and received packets following lost/corrupted packets. + */ +#define M_HIGHPRI 0x2000 /* output packet for sc_fastq */ +#define M_ERRMARK 0x4000 /* steal a bit in mbuf m_flags */ + +/* + * The following disgusting hack gets around the problem that IP TOS + * can't be set yet. We want to put "interactive" traffic on a high + * priority queue. To decide if traffic is interactive, we check that + * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control. + */ +static u_short interactive_ports[8] = { + 0, 513, 0, 0, + 0, 21, 0, 23, +}; + +enum { QFREE, QRAW, QFAST, QSLOW, QIN, QNP, QCACHE }; + +static struct qparms qparms[] = { + {20, 40, 50, "free"}, /* freeq */ + {5, 20, 25, "fast"}, /* rawq */ + {5, 20, 25, "slow"}, /* fastq */ + {5, 20, 25, "in"}, /* slowq */ + {5, 20, 25, "out"}, /* inq */ + {5, 20, 25, "np"}, /* npq */ + {0, COMPRESS_CACHE_LEN, COMPRESS_CACHE_LEN, "cache"} /* cache */ +}; + +#define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p)) + +#ifndef IPTOS_LOWDELAY +#define IPTOS_LOWDELAY 0x10 +#endif + +#ifdef PPP_COMPRESS +/* + * List of compressors we know about. + * We leave some space so maybe we can modload compressors. + */ + +extern struct compressor ppp_bsd_compress; + +struct compressor *ppp_compressors[8] = { +#if DO_BSD_COMPRESS + &ppp_bsd_compress, +#endif + NULL +}; +#endif /* PPP_COMPRESS */ + +/* yeah, we sometimes have to change the MTU after having created the + * device. Let's hope this doesn't break anything!!! + */ +#define if_mtu_set(ifn,mtu) (((struct ifnet *)ifn)->if_mtu = mtu) + +extern int ipforwarding; +extern int ipsendredirects; + +kern_server_t instance; + +/* + * Sigh. Should be defined in but isn't... + */ +union ifr_ifru { + short ifru_flags; + short ifru_mtu; + u_long ifru_asyncmap; + int ifru_metric; + caddr_t ifru_data; +}; + + +netbuf_t +pppgetbuf(netif_t ifp) +{ + netbuf_t nb; + + int len = MAX(if_mtu(ifp), PPP_MTU) + PPP_HDRLEN + PPP_FCSLEN; + nb = ppp_nb_alloc(len); + if (nb != NULL) + { + ppp_nb_shrink_top(nb, PPP_HDRLEN); + nb_shrink_bot(nb, PPP_FCSLEN); /* grown by pppstart() */ + } + return nb; +} + +/* + * Called from boot code to establish ppp interfaces. + */ +int +pppattach() +{ + register struct ppp_softc *sc; + register int i = 0; + + IOLog("\nPPP version 2.2 released for NS 3.2 and 3.3\n"); + IOLog("LKS: %s\n", sccsid); + IOLog("by Stephen Perkins, Philip Prindeville, and Pete French\n"); + + IOLog("Installing PPP on Line Discipline %d\n", PPPDISC); + if (install_ppp_ld() < 0) { + IOLog("ppp: Could not install line discipline\n"); + return 0; + } + + + IOLog("Installing interfaces:\n"); + for (sc = ppp_softc; i < NUM_PPP; sc++, i++) { + sc->sc_if = if_attach(NULL, NULL, pppoutput, + pppgetbuf, pppcontrol, "ppp", i, "Serial line PPP", + PPP_MTU, IFF_POINTOPOINT, NETIFCLASS_VIRTUAL, (void *) sc); + IOLog(" Initializing ppp%d\n", i); + nbq_init(&sc->sc_freeq, &qparms[QFREE]); + nbq_init(&sc->sc_rawq, &qparms[QRAW]); + nbq_init(&sc->sc_fastq, &qparms[QFAST]); + nbq_init(&sc->sc_slowq, &qparms[QSLOW]); + nbq_init(&sc->sc_inq, &qparms[QIN]); + nbq_init(&sc->sc_npq, &qparms[QNP]); + nbq_init(&sc->sc_compq, &qparms[QCACHE]); + IOLog(" ppp%d successfully attached.\n", i); +#ifdef NBPFILTER + bpfattach((caddr_t *) &sc->sc_bpf, sc->sc_if, DLT_PPP, PPP_HDRLEN); +#endif + } + + ipforwarding = 1; + ipsendredirects = 1; + IOLog("PPP-2.2 Successfully Installed.\n\n"); + return 1; +} + +int +pppdetach() +{ + struct ppp_softc *sc; + int i; + + IOLog("Removing PPP on Line Discipline %d\n", PPPDISC); + if (!tty_ld_remove(PPPDISC)) + IOLog("ppp: Could not remove line discipline\n"); + + IOLog("Removing interfaces:\n"); + for (sc = ppp_softc, i = 0; i < NUM_PPP; sc++, i++) { + nbq_free(&sc->sc_freeq); + nbq_free(&sc->sc_rawq); + nbq_free(&sc->sc_fastq); + nbq_free(&sc->sc_slowq); + nbq_free(&sc->sc_inq); + nbq_free(&sc->sc_npq); + nbq_free(&sc->sc_compq); + if_detach(sc->sc_if); + /* no idea why we need this, but... */ + bzero(sc->sc_if, sizeof(netif_t)); + IOLog(" ppp%d successfully detached.\n", i); + } + IOLog("PPP-2.2 Successfully Removed.\n\n"); + return 0; +} + +/* + * Allocate a ppp interface unit and initialize it. + */ +struct ppp_softc * +pppalloc(pid) + pid_t pid; +{ + int nppp, i; + struct ppp_softc *sc; + + for (nppp = 0, sc = ppp_softc; nppp < NUM_PPP; nppp++, sc++) + if (sc->sc_xfer == pid) { + IOLogDbg("ppp%d: alloc'ing unit %d to proc %d\n", nppp, nppp, pid); + sc->sc_xfer = 0; + return sc; + } + for (nppp = 0, sc = ppp_softc; nppp < NUM_PPP; nppp++, sc++) + if (sc->sc_devp == NULL) + break; + if (nppp >= NUM_PPP) + return NULL; + + sc->sc_flags = 0; + sc->sc_mru = PPP_MRU; + sc->sc_relinq = NULL; +#ifdef VJC + vj_compress_init(&sc->sc_comp, -1); +#endif +#ifdef PPP_COMPRESS + sc->sc_xc_state = NULL; + sc->sc_rc_state = NULL; +#endif /* PPP_COMPRESS */ + for (i = 0; i < NUM_NP; ++i) + sc->sc_npmode[i] = NPMODE_ERROR; + /* XXX - I'm not sure why the npqueue was zapped here... */ + sc->sc_last_sent = sc->sc_last_recv = time.tv_sec; + sc->sc_compsched = 0; + sc->sc_decompsched = 0; + +#if 0 + /* + * Clear PPP stats information for the new session + */ + sc->sc_bytessent = 0; + sc->sc_bytesrcvd = 0; + if_opackets_set(sc->sc_if, 0); + if_ipackets_set(sc->sc_if, 0); + if_oerrors_set(sc->sc_if, 0); + if_ierrors_set(sc->sc_if, 0); +#endif + + /* + * XXX -- We need to get packets here, and we don't care if we do block... + * We do this after we set the sc_mru. + */ + pppfillfreeq((void *) sc); + + return sc; +} + +/* + * Deallocate a ppp unit. Must be called at splnet or higher. + */ +void +pppdealloc(sc) + struct ppp_softc *sc; +{ + + if_flags_set(sc->sc_if, if_flags(sc->sc_if) & ~(IFF_UP|IFF_RUNNING)); + sc->sc_devp = NULL; + sc->sc_xfer = 0; + nbq_flush(&sc->sc_freeq); + nbq_flush(&sc->sc_rawq); + nbq_flush(&sc->sc_inq); + nbq_flush(&sc->sc_fastq); + nbq_flush(&sc->sc_slowq); + nbq_flush(&sc->sc_npq); + nbq_flush(&sc->sc_compq); +#ifdef PPP_COMPRESS + ppp_ccp_closed(sc); + sc->sc_xc_state = NULL; + sc->sc_rc_state = NULL; +#endif /* PPP_COMPRESS */ + + +} + +/* + * Ioctl routine for generic ppp devices. + */ +int +pppioctl(sc, cmd, data, flag) + struct ppp_softc *sc; + void *data; + u_long cmd; + int flag; +{ + struct proc *p = curproc; + int s, error, flags, mru, nb, npx, oldflags; + struct ppp_option_data *odp; + struct compressor **cp; + struct npioctl *npi; + time_t t; +#ifdef PPP_COMPRESS + u_char ccp_option[CCP_MAX_OPTION_LENGTH]; +#endif + netbuf_t m; +#ifdef HAS_BROKEN_TIOCSPGRP + struct tty *tp = sc->sc_devp; +#endif + + switch (cmd) { + case FIONREAD: + s = splimp(); /* paranoid; splnet probably ok */ + if ((m = nbq_peek(&sc->sc_inq)) != NULL) + *(int *)data = nb_size(m); + else + *(int *)data = 0; + splx(s); + break; + + case PPPIOCGUNIT: + *(int *)data = if_unit(sc->sc_if); +#if 0 + IOLogDbg("ppp%d: unit gets %d\n", if_unit(sc->sc_if), *(int *)data); +#endif + break; + + case PPPIOCGFLAGS: + *(u_int *)data = sc->sc_flags; +#if 0 + IOLogDbg("ppp%d: flags gets 0x%x\n", if_unit(sc->sc_if), *(u_int *)data); +#endif + break; + + case PPPIOCSFLAGS: + if (! suser()) + return EPERM; + flags = *(int *)data & SC_MASK; + s = splnet(); +#ifdef PPP_COMPRESS + if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN)) + ppp_ccp_closed(sc); +#endif + splimp(); + oldflags = sc->sc_flags; + sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags; + splx(s); +#if 0 + IOLogDbg("ppp%d: data 0x%x, flags: old 0x%x new 0x%x\n", + if_unit(sc->sc_if), flags, oldflags, sc->sc_flags); +#endif + break; + + case PPPIOCSMRU: + if (! suser()) + return EPERM; + mru = *(int *)data; +#if 0 + IOLogDbg("ppp%d: setting mru %d\n", if_unit(sc->sc_if), mru); +#endif + if (mru >= PPP_MRU && mru <= PPP_MAXMRU) { + + /* To make sure we handle the received packet + * correctly, we do two things. First, we + * empty out the free_q. We then remove + * the current input buffer, set the input length + * to zero, and set the flush flag. + */ + s = splimp(); + nbq_flush(&sc->sc_freeq); /* get rid of old buffers */ + sc->sc_mru = mru; + if (sc->sc_m){ + nb_free(sc->sc_m); + sc->sc_m = NULL; + if (sc->sc_ilen != 0) + sc->sc_flags |= SC_FLUSH; + } + sc->sc_ilen = 0; + splx(s); + pppfillfreeq((void *) sc); /* and make a queue of new ones */ + pppgetm(sc); + } + break; + + case PPPIOCGMRU: + *(int *)data = sc->sc_mru; +#if 0 + IOLogDbg("ppp%d: mru gets 0x%x\n", if_unit(sc->sc_if), *(int *)data); +#endif + break; + +#ifdef VJC + case PPPIOCSMAXCID: + if (! suser()) + return EPERM; +#if 0 + IOLogDbg("ppp%d: setting max cid %d\n", if_unit(sc->sc_if), *(int *)data); +#endif + s = splnet(); + vj_compress_init(&sc->sc_comp, *(int *)data); + splx(s); + break; +#endif + + case PPPIOCXFERUNIT: + if (! suser()) + return EPERM; + sc->sc_xfer = p->p_pid; +#if 0 + IOLogDbg("ppp%d: setting pid %d\n", if_unit(sc->sc_if), sc->sc_xfer); +#endif + break; + +#ifdef PPP_COMPRESS + case PPPIOCSCOMPRESS: + if (! suser()) + return EPERM; + odp = (struct ppp_option_data *) data; + nb = odp->length; + if (nb > sizeof(ccp_option)) + nb = sizeof(ccp_option); + if (error = copyin(odp->ptr, ccp_option, nb)) + return (error); + if (ccp_option[1] < 2) /* preliminary check on the length byte */ + return (EINVAL); + for (cp = ppp_compressors; *cp != NULL; ++cp) + if ((*cp)->compress_proto == ccp_option[0]) { + /* + * Found a handler for the protocol - try to allocate + * a compressor or decompressor. + */ + error = 0; + s = splnet(); + if (odp->transmit) { + if (sc->sc_xc_state != NULL) + (*sc->sc_xcomp->comp_free)(sc->sc_xc_state); + sc->sc_xcomp = *cp; + sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb); + if (sc->sc_xc_state == NULL) { + IOLogDbg("ppp%d: comp_alloc failed", if_unit(sc->sc_if)); + error = ENOBUFS; + } + splimp(); + sc->sc_flags &= ~SC_COMP_RUN; + } else { + if (sc->sc_rc_state != NULL) + (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state); + sc->sc_rcomp = *cp; + sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb); + if (sc->sc_rc_state == NULL) { + IOLogDbg("ppp%d: decomp_alloc failed", if_unit(sc->sc_if)); + error = ENOBUFS; + } + splimp(); + sc->sc_flags &= ~SC_DECOMP_RUN; + } + splx(s); + return (error); + } + IOLogDbg("ppp%d: no compressor for [%x %x %x], %x", if_unit(sc->sc_if), + ccp_option[0], ccp_option[1], ccp_option[2], nb); + return (EINVAL); /* no handler found */ +#endif /* PPP_COMPRESS */ + +#ifdef HAS_BROKEN_TIOCSPGRP + case TIOCSPGRP: +#if 0 + IOLogDbg("ppp%d: pgrp was %d, setting pgrp %d, flags 0x%x, state 0x%x\n", + if_unit(sc->sc_if), tp->t_pgrp, *(int *)data, + tp->t_flags, tp->t_state); +#endif + tp->t_pgrp = *(int *)data; + break; +#endif + + case PPPIOCGNPMODE: + case PPPIOCSNPMODE: + npi = (struct npioctl *) data; + switch (npi->protocol) { + case PPP_IP: + npx = NP_IP; + break; + default: + return EINVAL; + } + if (cmd == PPPIOCGNPMODE) { + npi->mode = sc->sc_npmode[npx]; + } else { + if (! suser()) + return EPERM; +#if 0 + IOLogDbg("ppp%d: setting protocol %d to mode %d\n", if_unit(sc->sc_if), npx, npi->mode); +#endif + if (npi->mode != sc->sc_npmode[npx]) { + s = splimp(); + sc->sc_npmode[npx] = npi->mode; + if (npi->mode != NPMODE_QUEUE) { + ppp_requeue(sc); + (*sc->sc_start)(sc); + } + splx(s); + } + } + break; + + case PPPIOCGIDLE: + s = splimp(); + t = time.tv_sec; + ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent; + ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv; + splx(s); + break; + + default: + return (-1); + } + return (0); +} + +int +pppcontrol(ifp, cmd, data) + netif_t ifp; + const char *cmd; + void *data; +{ +#if 0 + register struct ppp_softc *sc = &ppp_softc[if_unit(ifp)]; +#endif + + if (!strcmp(cmd, IFCONTROL_UNIXIOCTL)) { + if_ioctl_t* ctl = (if_ioctl_t*)data; + return pppsioctl(ifp, + ctl->ioctl_command, + ctl->ioctl_data); + } else if (!strcmp(cmd, IFCONTROL_SETADDR)) { + struct sockaddr_in *sin = (struct sockaddr_in *)data; + if (sin->sin_family != AF_INET) + return EAFNOSUPPORT; +#if 0 + IOLogDbg("ppp%d: setting address, and bringing up interface\n", + if_unit(ifp)); +#endif + if_flags_set(ifp, if_flags(ifp) | IFF_UP); + return 0; + } + /* + * We implement this to allow iftab + * to contain -AUTOMATIC- entries + * without generating errors at boot time. + * We do not, however, mark it as UP. + */ + else if (!strcmp(cmd, IFCONTROL_AUTOADDR)) { + struct sockaddr_in *sin = (struct sockaddr_in *) data; + if (sin->sin_family != AF_INET) + return EAFNOSUPPORT; +#if 0 + IOLogDbg("ppp%d: probing for interface address\n", if_unit(ifp)); +#endif + return 0; + } else if (!strcmp(cmd, IFCONTROL_SETFLAGS)) { + register union ifr_ifru *ifr = (union ifr_ifru *)data; + if (!suser()) + return EPERM; +#if 0 + IOLogDbg("ppp%d: old flags 0x%x, new flags 0x%x\n", if_unit(ifp), + if_flags(ifp), ifr->ifru_flags); +#endif + if_flags_set(ifp, ifr->ifru_flags); + return 0; + } + /* + * Under 3.2 developer, I don't know the symbol for this + * new 3.3 command. So it is a constant for now. I don't + * believe I need to do anything to support this at the moment. + */ + else if (strcmp(cmd, "add-multicast") == 0) { + struct sockaddr_in *sin = (struct sockaddr_in *) data; + if (sin->sin_family != AF_INET) + return EAFNOSUPPORT; + } else { + IOLog("ppp%d: Invalid ppp control %s\n", if_unit(ifp), cmd); + return EINVAL; + } +} + +/* + * Process an ioctl request to the ppp network interface. + */ +int +pppsioctl(ifp, cmd, data) + register netif_t ifp; + int cmd; + caddr_t data; +{ + register struct ppp_softc *sc = &ppp_softc[if_unit(ifp)]; + register struct ifaddr *ifa = (struct ifaddr *)data; + register struct ifreq *ifr = (struct ifreq *)data; + struct ppp_stats *psp; +#ifdef PPP_COMPRESS + struct ppp_comp_stats *pcp; +#endif + int s = splimp(), error = 0; + + switch (cmd) { + case SIOCSIFFLAGS: + IOLog("ppp%d: pppioctl: SIOCSIFFLAGS called!\n", if_unit(ifp)); +#if 0 + if_flags_set(ifp, (if_flags(ifp) & IFF_CANTCHANGE) + | (ifr->ifr_flags & ~IFF_CANTCHANGE)); +#endif + break; + + case SIOCSIFADDR: + if (ifa->ifa_addr.sa_family != AF_INET) + error = EAFNOSUPPORT; + break; + + case SIOCSIFDSTADDR: + if (ifa->ifa_addr.sa_family != AF_INET) + error = EAFNOSUPPORT; + break; + + case SIOCSIFMTU: + if (!suser()) { + error = EPERM; + break; + } + if_mtu_set(sc->sc_if, ifr->ifr_mtu); + nbq_flush(&sc->sc_freeq); /* get rid of old buffers */ + pppsched(pppfillfreeq, sc); /* and make a queue of new ones */ + pppgetm(sc); + break; + + case SIOCGIFMTU: + ifr->ifr_mtu = if_mtu(sc->sc_if); + break; + + case SIOCGPPPSTATS: + psp = &((struct ifpppstatsreq *) data)->stats; + bzero(psp, sizeof(*psp)); + psp->p.ppp_ibytes = sc->sc_bytesrcvd; + psp->p.ppp_ipackets = if_ipackets(sc->sc_if); + psp->p.ppp_ierrors = if_ierrors(sc->sc_if); + psp->p.ppp_obytes = sc->sc_bytessent; + psp->p.ppp_opackets = if_opackets(sc->sc_if); + psp->p.ppp_oerrors = if_oerrors(sc->sc_if); +#ifdef VJC + psp->vj.vjs_packets = sc->sc_comp.stats.vjs_packets; + psp->vj.vjs_compressed = sc->sc_comp.stats.vjs_compressed; + psp->vj.vjs_searches = sc->sc_comp.stats.vjs_searches; + psp->vj.vjs_misses = sc->sc_comp.stats.vjs_misses; + psp->vj.vjs_uncompressedin = sc->sc_comp.stats.vjs_uncompressedin; + psp->vj.vjs_compressedin = sc->sc_comp.stats.vjs_compressedin; + psp->vj.vjs_errorin = sc->sc_comp.stats.vjs_errorin; + psp->vj.vjs_tossed = sc->sc_comp.stats.vjs_tossed; +#endif /* VJC */ + break; + +#ifdef PPP_COMPRESS + case SIOCGPPPCSTATS: + pcp = &((struct ifpppcstatsreq *) data)->stats; + bzero(pcp, sizeof(*pcp)); + if (sc->sc_xc_state != NULL) + (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c); + if (sc->sc_rc_state != NULL) + (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d); + break; +#endif /* PPP_COMPRESS */ + + default: + error = EINVAL; + } + splx(s); + return (error); +} + +/* + * Queue a packet. Start transmission if not active. + * Packet is placed in Information field of PPP frame. + */ +int +pppoutput(ifp, m0, arg) + netif_t ifp; + netbuf_t m0; + void *arg; +{ + register struct ppp_softc *sc = &ppp_softc[if_unit(ifp)]; + struct sockaddr *dst = (struct sockaddr *) arg; + int protocol, address, control; + u_char *cp; + int s, error; + mark_t flags = 0; + struct ip *ip; + struct nb_queue *ifq; + enum NPmode mode; + + if (sc->sc_devp == NULL || (if_flags(ifp) & IFF_RUNNING) == 0 + || (if_flags(ifp) & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC) { + error = ENETDOWN; /* sort of */ + goto bad; + } + + /* + * Compute PPP header. + */ + flags &= ~M_HIGHPRI; + switch (dst->sa_family) { +#ifdef INET + case AF_INET: + address = PPP_ALLSTATIONS; + control = PPP_UI; + protocol = PPP_IP; + mode = sc->sc_npmode[NP_IP]; + + /* + * If this packet has the "low delay" bit set in the IP header, + * or TCP and to an interactive port, put it on the fastq instead + */ + ip = mtod(m0, struct ip *); + if (ip->ip_tos & IPTOS_LOWDELAY || ip->ip_p == IPPROTO_ICMP) + goto urgent; + else if (ip->ip_p == IPPROTO_TCP) { + register u_short *p = (u_short *) &(((caddr_t) ip)[ip->ip_hl << 2]); + if (INTERACTIVE(ntohs(p[0])) || INTERACTIVE(ntohs(p[1]))) +urgent: flags |= M_HIGHPRI; + } + break; +#endif +#ifdef NS + case AF_NS: + address = PPP_ALLSTATIONS; + control = PPP_UI; + protocol = PPP_XNS; + mode = NPMODE_PASS; + break; +#endif + case AF_UNSPEC: + address = PPP_ADDRESS(dst->sa_data); + control = PPP_CONTROL(dst->sa_data); + protocol = PPP_PROTOCOL(dst->sa_data); + mode = NPMODE_PASS; + break; + default: + IOLog("ppp%d: af%d not supported\n", if_unit(ifp), dst->sa_family); + error = EAFNOSUPPORT; + goto bad; + } + + /* + * Drop this packet, or return an error, if necessary. + */ + if (mode == NPMODE_ERROR) { + error = ENETDOWN; + goto bad; + } + if (mode == NPMODE_DROP) { + error = 0; + goto bad; + } + + /* + * Add PPP header. + */ + ppp_nb_grow_top(m0, PPP_HDRLEN); + + cp = mtod(m0, u_char *); + *cp++ = address; + *cp++ = control; + *cp++ = protocol >> 8; + *cp++ = protocol & 0xff; + + if (sc->sc_flags & SC_LOG_OUTPKT) { + IOLog("ppp%d: output:\n", if_unit(ifp)); /* XXX */ + pppdumpm(m0); + } + +#ifdef NBPFILTER + /* + * See if bpf wants to look at the packet. + */ + if (sc->sc_bpf) + bpf_tap(sc->sc_bpf, nb_map(m0), nb_size(m0)); +#endif + + /* + * Put the packet on the appropriate queue. + */ + s = splimp(); /* splnet should be OK now */ + if (mode == NPMODE_QUEUE) { + nb_set_mark(m0,flags); /* save priority */ + /* XXX we should limit the number of packets on this queue */ + nbq_enqueue(&sc->sc_npq, m0); /* XXX is this correct? */ + } else { + ifq = (flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_slowq; + if (nbq_full(ifq) < 0) { + nbq_drop(ifq); + IOLog("ppp%d: output queue full\n", if_unit(sc->sc_if)); + splx(s); + incr_cnt(sc->sc_if, if_oerrors); + error = ENOBUFS; + goto bad; + } + nbq_enqueue(ifq, m0); + } + + /* + * If we don't have some compressed packets already + * and we are not at interrupt priority, then do some compression. + * + * We need to be especially careful here. pppouput() is typically + * called at 2 different priority levels. On a NeXT, neither of these + * is the interrupt priority level. However, on Intel, one of them is. + * I don't know about HPPA or Sparc. Simple fix is to just check. + */ + + if(!sc->sc_compsched && s != ipltospl(IPLIMP)) { + sc->sc_compsched = 1; + splx(s); + pppintr_comp(sc); /* Calls pppstart() */ + } + else { + (*sc->sc_start)(sc); + splx(s); + } + + return (0); + +bad: + nb_free(m0); + return (error); +} + +/* + * After a change in the NPmode for some NP, move packets from the + * npqueue to the send queue or the fast queue as appropriate. + * Should be called at splimp (actually splnet would probably suffice). + * Due to some of the uglies in the packet queueing system I have + * implemented this without the mpp stuff. + * PCF + */ + +static void +ppp_requeue(sc) + struct ppp_softc *sc; +{ + netbuf_t m, lm, nm; + struct nb_queue *ifq; + enum NPmode mode; + mark_t flags; + + lm = nm = NULL; + for (m = sc->sc_npq.head; m; ) { + nb_get_next(m,&nm); + + switch (PPP_PROTOCOL(mtod(m, u_char *))) { + case PPP_IP: + mode = sc->sc_npmode[NP_IP]; + break; + default: + mode = NPMODE_PASS; + } + + switch (mode) { + case NPMODE_PASS: + /* + * This packet can now go on one of the queues to be sent. + */ + if(lm) + nb_set_next(lm,nm); + else + sc->sc_npq.head = nm; + nb_set_next(m,NULL); + nb_get_mark(m,&flags); + ifq = (flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_slowq; + if (nbq_full(ifq)) { + nbq_drop(ifq); + incr_cnt(sc->sc_if, if_oerrors); + nb_free(m); + } else + nbq_enqueue(ifq, m); + sc->sc_npq.len--; + break; + + case NPMODE_DROP: + case NPMODE_ERROR: + sc->sc_npq.len--; + nb_free(m); + break; + + case NPMODE_QUEUE: + lm = m; + break; + } + m = nm; + } + sc->sc_npq.tail = lm; /* anything further on has been sent ! */ +} + +/* + * Get a packet to send. This procedure is intended to be called + * at spltty()/splimp(), so it takes little time. If there isn't + * a packet waiting to go out, it schedules a software interrupt + * to prepare a new packet; the device start routine gets called + * again when a packet is ready. + */ +netbuf_t +ppp_dequeue(sc) + struct ppp_softc *sc; +{ + netbuf_t m; + int error; + + m = nbq_dequeue(&sc->sc_compq); + + + if (!sc->sc_compsched && + (! nbq_empty(&sc->sc_slowq) || ! nbq_empty(&sc->sc_fastq))) + { + + if ((error = pppsched(pppintr_comp, sc)) == KERN_SUCCESS) + sc->sc_compsched = 1; + else + { + IOLogDbg("ppp%d: compression callout failed returning %d\n", + if_unit(sc->sc_if), error); + } + } + + return m; +} + +/* + * Takes all received input packets and uncompresses/hands_off. + * Must not be reentrant and is called at normal priority. + * Guaranteed Non-Reentrancy means we don't need to be at splnet(). + * + */ + +void +pppintr_decomp(arg) + void *arg; +{ + struct ppp_softc *sc = (struct ppp_softc *)arg; + int s; + netbuf_t m; + + if (nbq_low(&sc->sc_freeq)) + pppfillfreeq((void *) sc); + + decomp: + for (;;) { + m = nbq_dequeue(&sc->sc_rawq); + if (m == NULL) + break; + ppp_inproc(sc, m); + } + + /* + * Now we have aparently emptied the queue. So, we try to reset the + * synchronization flag that schedules callbacks. We check for the + * possibility that an interrupt occurred before we finish this check. + */ + s = splimp(); + if (!nbq_empty(&sc->sc_rawq)) + { + splx(s); + goto decomp; + } + else + { + sc->sc_decompsched = 0; + splx(s); + } +} + + + +/* + * Readies the next few output packet from + * the sc_fastq/sc_slowq. Will try to + * precompress all packets on the fast + * queue and at most one from the slow queue. + */ +void +pppintr_comp(arg) + void *arg; +{ + struct ppp_softc *sc = (struct ppp_softc *)arg; + int s; + netbuf_t m; + + if (nbq_low(&sc->sc_freeq)) + pppfillfreeq((void *) sc); + + + while (!nbq_full(&sc->sc_compq) && !nbq_empty(&sc->sc_fastq)) + ppp_outpkt(sc); + + if (!nbq_full(&sc->sc_compq) && !nbq_empty(&sc->sc_slowq)) + ppp_outpkt(sc); + + sc->sc_compsched = 0; +} + +/* + * Grab another packet off a queue and apply VJ compression, + * packet compression, address/control and/or protocol compression + * if enabled. Should be called at splnet. + */ +static void +ppp_outpkt(sc) + struct ppp_softc *sc; +{ + int s; + netbuf_t m; + u_char *cp; + int address, control, protocol; + + /* + * Grab a packet to send: first try the fast queue, then the + * normal queue. + */ + m = nbq_dequeue(&sc->sc_fastq); + if (m == NULL) + m = nbq_dequeue(&sc->sc_slowq); + if (m == NULL) + return; + + /* + * Extract the ppp header of the new packet. + * The ppp header will be in one netbuf. + */ + cp = mtod(m, u_char *); + address = PPP_ADDRESS(cp); + control = PPP_CONTROL(cp); + protocol = PPP_PROTOCOL(cp); + + switch (protocol) { + case PPP_IP: + /* + * Update the time we sent the most recent packet. + */ + sc->sc_last_sent = time.tv_sec; + +#ifdef VJC + /* + * If the packet is a TCP/IP packet, see if we can compress it. + */ + if (sc->sc_flags & SC_COMP_TCP) { + struct ip *ip; + int type; + u_char *vjhdr; + + ip = (struct ip *) (cp + PPP_HDRLEN); + /* this code assumes the IP/TCP header is in one netbuf */ + if (ip->ip_p == IPPROTO_TCP) { + type = vj_compress_tcp(ip, nb_size(m) - PPP_HDRLEN, + &sc->sc_comp, + !(sc->sc_flags & SC_NO_TCP_CCID), &vjhdr); + switch (type) { + case TYPE_UNCOMPRESSED_TCP: + protocol = PPP_VJC_UNCOMP; + break; + case TYPE_COMPRESSED_TCP: + ppp_nb_shrink_top(m, vjhdr - (u_char *) ip); + protocol = PPP_VJC_COMP; + cp = mtod(m, u_char *); + cp[0] = address; /* header has moved */ + cp[1] = control; + cp[2] = 0; + break; + } + cp[3] = protocol; /* update protocol in PPP header */ + } + } +#endif /* VJC */ + break; + +#ifdef PPP_COMPRESS + case PPP_CCP: + ppp_ccp(sc, m, 0); + break; +#endif /* PPP_COMPRESS */ + } + +#ifdef PPP_COMPRESS + if (protocol != PPP_LCP && protocol != PPP_CCP + && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) { + netbuf_t mcomp; + int slen, clen; + + slen = nb_size(m); + clen = (*sc->sc_xcomp->compress) + (sc->sc_xc_state, &mcomp, m, slen, + sc->sc_flags & SC_CCP_UP? if_mtu(sc->sc_if): 0); + if (mcomp != NULL) { + nb_free(m); + m = mcomp; + cp = mtod(m, u_char *); + protocol = cp[3]; + } + } +#endif /* PPP_COMPRESS */ + + /* + * Compress the address/control and protocol, if possible. + */ + if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS && + control == PPP_UI && protocol != PPP_ALLSTATIONS && + protocol != PPP_LCP) { + /* can compress address/control */ + ppp_nb_shrink_top(m, 2); + } + if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) { + /* can compress protocol */ + if (mtod(m, u_char *) == cp) { + cp[2] = cp[1]; /* move address/control up */ + cp[1] = cp[0]; + } + ppp_nb_shrink_top(m, 1); + } + + s = splimp(); + nbq_enqueue(&sc->sc_compq, m); + (*sc->sc_start)(sc); + splx(s); +} + +#ifdef PPP_COMPRESS +/* + * Handle a CCP packet. `rcvd' is 1 if the packet was received, + * 0 if it is about to be transmitted. + */ +static void +ppp_ccp(sc, m, rcvd) + struct ppp_softc *sc; + netbuf_t m; + int rcvd; +{ + u_char *dp, *ep; + int slen, s; + + /* + * Get a pointer to the data after the PPP header. + */ + dp = mtod(m, u_char *) + PPP_HDRLEN; + + ep = mtod(m, u_char *) + nb_size(m); + if (dp + CCP_HDRLEN > ep) + return; + slen = CCP_LENGTH(dp); + if (dp + slen > ep) { + IOLogDbg("ppp%d: ccp: not enough data in netbuf (%x+%x > %x+%x)\n", + if_unit(sc->sc_if), dp, slen, mtod(m, u_char *), nb_size(m)); + return; + } + + switch (CCP_CODE(dp)) { + case CCP_CONFREQ: + case CCP_TERMREQ: + case CCP_TERMACK: + /* CCP must be going down - disable compression */ + if (sc->sc_flags & SC_CCP_UP) { + s = splimp(); + sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN); + splx(s); + } + break; + + case CCP_CONFACK: + if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP) + && slen >= CCP_HDRLEN + CCP_OPT_MINLEN + && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) { + if (!rcvd) { + /* we're agreeing to send compressed packets. */ + if (sc->sc_xc_state != NULL + && (*sc->sc_xcomp->comp_init) + (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN, + if_unit(sc->sc_if), 0, sc->sc_flags & SC_DEBUG)) { + s = splimp(); + sc->sc_flags |= SC_COMP_RUN; + splx(s); + } + } else { + /* peer is agreeing to send compressed packets. */ + if (sc->sc_rc_state != NULL + && (*sc->sc_rcomp->decomp_init) + (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN, + if_unit(sc->sc_if), +#ifdef VJC + VJ_HDRLEN + +#endif + 0, sc->sc_mru, sc->sc_flags & SC_DEBUG)) { + s = splimp(); + sc->sc_flags |= SC_DECOMP_RUN; + sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR); + splx(s); + } + } + } + break; + + case CCP_RESETACK: + if (sc->sc_flags & SC_CCP_UP) { + if (!rcvd) { + if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) { + (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state); + nbq_flush(&sc->sc_compq); /* Flush pre-compressed packets */ + } + } else { + if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) { + (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state); + s = splimp(); + sc->sc_flags &= ~SC_DC_ERROR; + splx(s); + } + } + } + break; + } +} + +/* + * CCP is down; free (de)compressor state if necessary. + */ +static void +ppp_ccp_closed(sc) + struct ppp_softc *sc; +{ + if (sc->sc_xc_state) { + (*sc->sc_xcomp->comp_free)(sc->sc_xc_state); + sc->sc_xc_state = NULL; + } + if (sc->sc_rc_state) { + (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state); + sc->sc_rc_state = NULL; + } +} +#endif /* PPP_COMPRESS */ + +/* + * PPP packet input routine. + * The caller has checked and removed the FCS and has inserted + * the address/control bytes and the protocol high byte if they + * were omitted. + */ +void +ppppktin(sc, m, lost) + struct ppp_softc *sc; + netbuf_t m; + int lost; +{ + int error, s = splimp(); + + nb_set_mark(m,(lost ? M_ERRMARK : 0)); + + /* XXX - we should check for the raw queue overflowing... */ + nbq_enqueue(&sc->sc_rawq, m); + if (!sc->sc_decompsched) + { + if ((error = pppsched(pppintr_decomp, sc)) == KERN_SUCCESS) + sc->sc_decompsched = 1; + else + IOLogDbg("ppp%d: decompression callout failed returning %d\n", + if_unit(sc->sc_if), error); + } + + splx(s); +} + +/* + * Process a received PPP packet, doing decompression as necessary. + */ +#define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \ + TYPE_UNCOMPRESSED_TCP) + +static void +ppp_inproc(sc, m) + struct ppp_softc *sc; + netbuf_t m; +{ + struct nb_queue *inq; + int s, ilen, xlen, proto, rv; + mark_t flags; + u_char *cp, adrs, ctrl; + netbuf_t dmp; + u_char *iphdr; + u_int hlen; + + incr_cnt(sc->sc_if, if_ipackets); + + nb_get_mark(m,&flags); + + if (sc->sc_flags & SC_LOG_INPKT) { + IOLog("ppp%d: got %d bytes\n", if_unit(sc->sc_if), nb_size(m)); + pppdumpm(m); + } + + cp = mtod(m, u_char *); + adrs = PPP_ADDRESS(cp); + ctrl = PPP_CONTROL(cp); + proto = PPP_PROTOCOL(cp); + + if (flags & M_ERRMARK) { + s = splimp(); + sc->sc_flags |= SC_VJ_RESET; + splx(s); + } + +#ifdef PPP_COMPRESS + /* + * Decompress this packet if necessary, update the receiver's + * dictionary, or take appropriate action on a CCP packet. + */ + if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN) + && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) { + /* decompress this packet */ + rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp); + if (rv == DECOMP_OK){ + nb_free(m); + if (dmp == NULL){ + /* No error, but no decompressed packet returned */ + return; + } + m = dmp; + cp = mtod(m, u_char *); + proto = PPP_PROTOCOL(cp); + + } else { + /* + * An error has occurred in decompression. + * Pass the compressed packet up to pppd, which may take + * CCP down or issue a Reset-Req. + */ + IOLogDbg("ppp%d: decompress failed %d\n", if_unit(sc->sc_if), rv); + s = splimp(); + sc->sc_flags |= SC_VJ_RESET; + if (rv == DECOMP_ERROR) + sc->sc_flags |= SC_DC_ERROR; + else + sc->sc_flags |= SC_DC_FERROR; + splx(s); + } + + } else { + if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) { + (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m); + } + if (proto == PPP_CCP) { + ppp_ccp(sc, m, 1); + } + } +#endif + + ilen = nb_size(m); + +#ifdef VJC + if (sc->sc_flags & SC_VJ_RESET) { + /* + * If we've missed a packet, we must toss subsequent compressed + * packets which don't have an explicit connection ID. + */ + vj_uncompress_err(&sc->sc_comp); + s = splimp(); + sc->sc_flags &= ~SC_VJ_RESET; + splx(s); + } + + /* + * See if we have a VJ-compressed packet to uncompress. + */ + if (proto == PPP_VJC_COMP) { + if (sc->sc_flags & SC_REJ_COMP_TCP) + goto bad; + + xlen = vj_uncompress_tcp(cp + PPP_HDRLEN, ilen - PPP_HDRLEN, + ilen - PPP_HDRLEN, + &sc->sc_comp, &iphdr, &hlen); + + if (xlen <= 0) { + IOLogDbg("ppp%d: VJ uncompress failed on type comp\n", + if_unit(sc->sc_if)); + goto bad; + } + + ppp_nb_grow_top(m, hlen - xlen); + nb_write(m, PPP_HDRLEN, hlen, iphdr); + + cp = mtod(m, u_char *); + cp[0] = adrs; + cp[1] = ctrl; + cp[2] = 0; + cp[3] = PPP_IP; + proto = PPP_IP; + + ilen += hlen - xlen; + + } else if (proto == PPP_VJC_UNCOMP) { + if (sc->sc_flags & SC_REJ_COMP_TCP) + goto bad; + + vj_uncompress_uncomp(cp + PPP_HDRLEN, &sc->sc_comp); + + proto = PPP_IP; + cp[3] = PPP_IP; + } +#endif /* VJC */ + +#ifdef NBPFILTER + /* See if bpf wants to look at the packet. */ + if (sc->sc_bpf) + bpf_tap(sc->sc_bpf, nb_map(m), nb_size(m)); +#endif + + rv = 0; + switch (proto) { +#ifdef INET + case PPP_IP: + /* + * IP packet - take off the ppp header and pass it up to IP. + */ + if ((if_flags(sc->sc_if) & IFF_UP) == 0 + || sc->sc_npmode[NP_IP] != NPMODE_PASS) { + /* interface is down - drop the packet. */ + nb_free(m); + IOLogDbg("ppp%d: IP packed dropped (NPmode)\n", if_unit(sc->sc_if)); + return; + } + ppp_nb_shrink_top(m, PPP_HDRLEN); + inet_queue(sc->sc_if, m); + sc->sc_last_recv = time.tv_sec; /* update time of last pkt rcvd */ + return; +#endif + + default: + /* + * Some other protocol - place on input queue for read(). + */ + inq = &sc->sc_inq; + rv = 1; + break; + } + + /* + * Put the packet on the appropriate input queue. + */ + s = splimp(); + if (nbq_full(inq)) { + nbq_drop(inq); + splx(s); + IOLog("ppp%d: input queue full\n", if_unit(sc->sc_if)); + goto bad; + } + nbq_enqueue(inq, m); + splx(s); + + if (rv) + (*sc->sc_ctlp)(sc); + + return; + + bad: + nb_free(m); + incr_cnt(sc->sc_if, if_ierrors); +} + +#define MAX_DUMP_BYTES 128 + +static void +pppdumpm(m0) + netbuf_t m0; +{ + char buf[3*MAX_DUMP_BYTES+4]; + char *bp = buf; + static char digits[] = "0123456789abcdef"; + int l = nb_size(m0); + u_char *rptr = mtod(m0, u_char *); + + while (l--) { + if (bp > buf + sizeof(buf) - 4) + goto done; + *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */ + *bp++ = digits[*rptr++ & 0xf]; + } + + *bp++ = ' '; +done: + if (l) + *bp++ = '>'; + *bp = 0; + IOLog("%s\n", buf); +} + + diff --git a/NeXT/if_pppvar.h b/NeXT/if_pppvar.h new file mode 100644 index 0000000..49a1fc7 --- /dev/null +++ b/NeXT/if_pppvar.h @@ -0,0 +1,104 @@ +/* + * if_ppp.h - Point-to-Point Protocol definitions. + * + * Copyright (c) 1989 Carnegie Mellon University. + * All rights reserved. + * + * Redistribution and use in source and binary forms are permitted + * provided that the above copyright notice and this paragraph are + * duplicated in all such forms and that any documentation, + * advertising materials, and other materials related to such + * distribution and use acknowledge that the software was developed + * by Carnegie Mellon University. The name of the + * University may not be used to endorse or promote products derived + * from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED + * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. + */ + +/* Portions Copyright (C) 1990 Brad K. Clements (streams support) + */ + +#import + +/* + * Supported network protocols. These values are used for + * indexing sc_npmode. + */ +#define NP_IP 0 /* Internet Protocol */ +#define NUM_NP 1 /* Number of NPs. */ + +#include "nbq.h" + + +/* only defined in the posix universe... */ +typedef int pid_t; +typedef unsigned short u_int16_t; + +struct ppp_softc { + netif_t sc_if; /* network-visible interface */ + u_int sc_flags; /* control/status bits; see if_ppp.h */ + struct tty *sc_devp; /* pointer to device-dep structure */ + void (*sc_start) __P((struct ppp_softc *)); /* start output proc */ + void (*sc_ctlp) __P((struct ppp_softc *)); /* rcvd control pkt */ + void (*sc_relinq) __P((struct ppp_softc *)); /* relinquish ifunit */ + u_int16_t sc_mru; /* max receive unit */ + pid_t sc_xfer; /* used in transferring unit */ + netbuf_t sc_m; /* Current TTY input netbuf */ + struct nb_queue sc_freeq; /* reserve netbufs */ + struct nb_queue sc_rawq; /* Raw input buffers */ + struct nb_queue sc_fastq; /* For telnet, rlogin, and ftp control */ + struct nb_queue sc_slowq; /* Everything else */ + struct nb_queue sc_inq; /* Input available to user ppp */ + struct nb_queue sc_npq; /* output packets not to be sent yet */ + struct nb_queue sc_compq; /* Cache of compressed bufs to be sent */ +#ifdef VJC + struct vjcompress sc_comp; +#endif + u_int sc_bytessent; /* count of octets sent */ + u_int sc_bytesrcvd; /* count of octets received */ +#if NBPFILTER + caddr_t sc_bpf; /* hook for BPF */ + int if_pcount; /* Really belongs in ifnet_t */ +#endif + enum NPmode sc_npmode[NUM_NP]; /* what to do with each NP */ +#ifdef PPP_COMPRESS + struct compressor *sc_xcomp; /* transmit compressor */ + void *sc_xc_state; /* transmit compressor state */ + struct compressor *sc_rcomp; /* receive decompressor */ + void *sc_rc_state; /* receive decompressor state */ +#endif + time_t sc_last_sent; /* time (secs) last NP pkt sent */ + time_t sc_last_recv; /* time (secs) last NP pkt rcvd */ + + short sc_compsched; /* synchronize compression callouts */ + short sc_decompsched; /* synchronize decompression callouts */ + + /* Device-dependent part for async lines. */ + ext_accm sc_asyncmap; /* async control character map */ + u_int32_t sc_rasyncmap; /* receive async control char map */ + netbuf_t sc_outm; /* netbuf currently being output */ + char *sc_mp; /* ptr to next char in input netbuf */ + u_int16_t sc_ilen; /* length of input packet so far */ + u_int16_t sc_fcs; /* FCS so far (input) */ + u_int16_t sc_outfcs; /* FCS so far for output packet */ + u_char sc_rawin[16]; /* chars as received */ + int sc_rawin_count; /* # in sc_rawin */ +}; + +extern struct ppp_softc ppp_softc[]; + +struct ppp_softc *pppalloc __P((pid_t pid)); +void pppdealloc __P((struct ppp_softc *sc)); +int pppioctl __P((struct ppp_softc *sc, u_long cmd, void *data, int flag)); +void ppppktin __P((struct ppp_softc *sc, netbuf_t m, int lost)); +netbuf_t ppp_dequeue __P((struct ppp_softc *sc)); + +#define t_sc T_LINEP + +#define incr_cnt(ifp,field) field##_set(ifp, field(ifp) + 1) + +#ifdef VJC +#define VJ_HDRLEN 128 +#endif diff --git a/NeXT/inlines.h b/NeXT/inlines.h new file mode 100644 index 0000000..46b0299 --- /dev/null +++ b/NeXT/inlines.h @@ -0,0 +1,243 @@ +/* + * Netbufs don't come with nifty queuing functions + * like mbufs. We therefore make our own quques by + * squirreling away an extra pointer before the data + * in a netbuf. As we can't guarantee that this will + * be aligned to anything in particular I use bcopy to + * read and write it. bcopy can use 32 bit if it really + * feels like it... + * PCF + * + +#if defined(m68k) +#import "spl.h" +#else +#import +#endif +#include +#include +#include "nbq.h" + +/* + * There is no driver kit for the Moto release. + */ +#ifndef IOLog +#define IOLog printf +#define IOLogDbg if (sc->sc_flags & SC_DEBUG) printf +#else +#define IOLogDbg if (sc->sc_flags & SC_DEBUG) IOLog +#endif + +extern kern_server_t instance; + +/* + * Careful about using this function. Some places + * in the code drop packets based on this count + * but they never free them. + */ + +static inline int +nbq_full(struct nb_queue* nbq) +{ + int rv; + rv = (nbq->len >= nbq->max); + return rv; +} + +static inline int +nbq_empty(struct nb_queue* nbq) +{ + int rv; + rv = (!nbq->head); + return rv; +} + +static inline int +nbq_low(struct nb_queue* nbq) +{ + int rv; + rv = (nbq->len <= nbq->low); + return rv; +} + +static inline int +nbq_high(struct nb_queue* nbq) +{ + int rv; + rv = (nbq->len >= nbq->high); + return rv; +} + +static inline netbuf_t +nbq_peek(struct nb_queue* nbq) +{ + int s; + netbuf_t nb; + + s = splimp(); + nb = nbq->head; + splx(s); + return nb; +} + +static inline netbuf_t +nbq_dequeue(struct nb_queue* nbq) +{ + int s; + netbuf_t nb; + + if (!nbq->head) + return NULL; + + s = splimp(); + nb = nbq->head; + nb_get_next(nb,&nbq->head); + if (!nbq->head) + nbq->tail = NULL; + --nbq->len; + splx(s); + + return nb; +} + +/* + * One simple note about nbq_enqueue: it will enqueue packets even if + * it is full, so the caller is responsible for checking this first... + * + * We return 1 if we added, else we return 0 + * if there was a problem. We leave it up to the caller + * to detect an error return value (0) and print + * an appropriate message/update stats. However, in the spirit of + * keeping the code as close to the netbsd version as is possible, + * WE WILL FREE a packet that can't be enqueued. This should be the + * responsibility of the caller but that is currently not the case. + * + * Actually, now I'm using the hidden pointer arrangement then theres + * no circumstances under which this can return 0, oh well... + * PCF + */ + +static inline int +nbq_enqueue(struct nb_queue* nbq, netbuf_t nb) +{ + int s; + + nb_set_next(nb,NULL); + s = splimp(); + if (nbq->tail) + nb_set_next(nbq->tail,nb); + else + nbq->head = nb; + nbq->tail = nb; + ++nbq->len; + splx(s); + return 1; +} + +static inline void +nbq_flush(struct nb_queue *nbq) +{ + netbuf_t nb,temp; + int s; + + s = splimp(); + nb = nbq->head; + while(nb) { + temp=nb; + nb_get_next(nb,&nb); + nb_free(temp); + } + + nbq->head = nbq->tail = NULL; + nbq->len = 0; + nbq->dropped = 0; + splx(s); +} + +/* + * Must not be called at interrupt priority + */ + +static inline void +nbq_init(struct nb_queue *nbq, struct qparms *qp) +{ + nbq->name = qp->q_name; + nbq->head = nbq->tail = NULL; + nbq->low = qp->q_low; + nbq->high = qp->q_high; + nbq->max = qp->q_max; + nbq->len = 0; + nbq->dropped = 0; +} + +static inline void +nbq_free(struct nb_queue *nbq) +{ + nbq_flush(nbq); +} + +static inline void +nbq_drop(struct nb_queue *nbq) +{ + ++nbq->dropped; +} + +/* + * Not very pretty, but it makes for less "diffs"... + */ +#define mtod(m,type) ((type) nb_map(m)) + +typedef void (*pfv)(void *); + +/* used by both ppp_tty.c and if_ppp.c */ +static inline kern_return_t +pppsched(pfv func, struct ppp_softc *sc) +{ + extern kern_server_t instance; + kern_return_t result; + + if ((result = kern_serv_callout(&instance, func, (void *)sc)) != KERN_SUCCESS) + IOLog("kern_serv_callout failed: ret = %x\n", result); + + return result; +} + +#undef u + +static inline thread_t +current_thread(void) +{ + extern thread_t active_threads[]; + + return active_threads[0]; +} + +extern struct proc *proc_from_thread(thread_t); +extern struct uthread *uthread_from_thread(thread_t); + +#define curproc (proc_from_thread(current_thread())) + +#ifdef NBPFILTER +#include + +extern struct bpf_fns fnarg; + +static inline void +bpfattach(caddr_t *driverp, netif_t ifp, u_int dlt, u_int hdrlen) +{ + struct bpf_attachargs atarg = {driverp, (caddr_t) ifp, dlt, hdrlen}; + + if (cdevsw[BPF_MAJOR_CHAR].d_ioctl != 0) + { + (*cdevsw[BPF_MAJOR_CHAR].d_ioctl)(0, BIOCATTACH, &atarg, 0); + (*cdevsw[BPF_MAJOR_CHAR].d_ioctl)(0, BIOCGFNS, &fnarg, 0); + } +} + + +static inline void +bpf_tap(caddr_t arg, u_char *pkt, u_int pktlen) +{ + (*fnarg.tapfn)(arg, pkt, pktlen); +} +#endif diff --git a/NeXT/linedisc.h b/NeXT/linedisc.h new file mode 100644 index 0000000..a7147ef --- /dev/null +++ b/NeXT/linedisc.h @@ -0,0 +1,72 @@ +/* + * linedisc.h -- includes for use with loadable line disciplines + */ +#define KERNEL 1 +#define KERNEL_FEATURES 1 + +#ifdef m68k +#import +#endif + +#import +/* +#import +*/ +#import +#import +#import +#import +/* +#import +#import +*/ +#import +#import +#import +#import +#import + +/* +#import +*/ +#ifdef m68k +#include "spl.h" +#endif + +#if NeXT +/* +#import +#import +*/ +#endif NeXT + +/* + * Line discipline "kind" + * NORMAL_LDISC -- Normal line disciplines use tty struct clists in + * standard manner + * SPECIAL_LDISC -- Special line disciplines have private buffering + * strategy + */ +#define NORMAL_LDISC 0 +#define SPECIAL_LDISC 1 + +extern int tty_ld_install( + int ld_number, + int ld_kind, + int (*ld_open)(dev_t dev, struct tty *tp), + void (*ld_close)(struct tty *tp), + int (*ld_read)(struct tty *tp, struct uio *uiop), + int (*ld_write)(struct tty *tp, struct uio *uiop), + int (*ld_ioctl)(struct tty *tp, int command, void *dataptr, int flag), + void (*ld_rint)(int c, struct tty *tp), + void (*ld_rend)(char *cp, u_int n, struct tty *tp), + void (*ld_start)(struct tty *tp), + int (*ld_modem)(struct tty *tp, int dcd_on), + int (*ld_select)(struct tty *tp, int rw) +); +extern int tty_ld_remove(int ld_number); +extern void ttydevstart(struct tty *tp); +extern void ttydevstop(struct tty *tp); +extern void ttyselwait(struct tty *tp, int rw); +extern void ttselwakeup(struct tty *tp); + diff --git a/NeXT/nbq.h b/NeXT/nbq.h new file mode 100644 index 0000000..73f7c1c --- /dev/null +++ b/NeXT/nbq.h @@ -0,0 +1,97 @@ +/* + * All very trivial - the simpler the better I say. We try and keep + * quqes of netbufs by squirreling a pointer away below the data area. + * This is done by the ppp_nb_alloc function. As long as everyone + * uses the ppp shrink and grow functions we should be o.k. This code + * has now been modified to keep the mark_t stuff nhere as well since + * we probably shafted that good and proper in the last version. oops ! + * PCF + */ + +#ifndef __NBQ_H__ +#define __NBQ_H__ + +#define KERNEL 1 + +#include +#include +#include "netbuf.h" + +struct qparms { + u_char q_low, q_high, q_max; + char *q_name; +}; + +struct nb_queue { + char *name; + int low, high, max, len, dropped; + netbuf_t head, tail; +}; + +typedef u_int mark_t; +#define NB_EXTRA (sizeof(mark_t)+sizeof(netbuf_t)) + +static inline void +nb_set_next(netbuf_t nb, netbuf_t ptr) +{ +if(nb) bcopy(&ptr,nb_map(nb)-sizeof(netbuf_t),sizeof(netbuf_t)); +} + +static inline void +nb_get_next(netbuf_t nb, netbuf_t *ptr) +{ +if(nb && ptr) bcopy(nb_map(nb)-sizeof(netbuf_t),ptr,sizeof(netbuf_t)); +} + +static inline void +nb_set_mark(netbuf_t nb, mark_t ptr) +{ +if(nb) bcopy(&ptr,nb_map(nb)-NB_EXTRA,sizeof(mark_t)); +} + +static inline void +nb_get_mark(netbuf_t nb, mark_t *ptr) +{ +if(nb && ptr) bcopy(nb_map(nb)-NB_EXTRA,ptr,sizeof(mark_t)); +} + +static inline void +ppp_nb_shrink_top(netbuf_t nb, unsigned int size) +{ + netbuf_t ptr; + mark_t mark; + nb_get_next(nb,&ptr); + nb_get_mark(nb,&mark); + nb_shrink_top(nb,size); + nb_set_mark(nb,mark); + nb_set_next(nb,ptr); +} + +static inline void +ppp_nb_grow_top(netbuf_t nb, unsigned int size) +{ + netbuf_t ptr; + mark_t mark; + nb_get_next(nb,&ptr); + nb_get_mark(nb,&mark); + nb_grow_top(nb,size); + nb_set_mark(nb,mark); + nb_set_next(nb,ptr); +} + +static inline netbuf_t +ppp_nb_alloc(unsigned int size) +{ + netbuf_t nb; + + size+=NB_EXTRA; + nb=nb_alloc(size); + if(nb) { + nb_shrink_top(nb,NB_EXTRA); + nb_set_next(nb,NULL); + nb_set_mark(nb,0); + } + return nb; +} +#endif /* __NBQ_H__ */ + diff --git a/NeXT/netbuf.h b/NeXT/netbuf.h new file mode 100644 index 0000000..578ac8f --- /dev/null +++ b/NeXT/netbuf.h @@ -0,0 +1,41 @@ +/* + * Copyright (C) 1990 by NeXT, Inc., All Rights Reserved + * + */ + +/* + * Network Buffer API (for kernel use only) + * + * HISTORY + * 09-Apr-90 Bradley Taylor (btaylor) at NeXT, Inc. + * Created. + */ +#ifndef _NETBUF_ +#define _NETBUF_ + +/* + * We know only that the first entry in the structure is a + * pointer that isn't used while the netbuf is allocated. + * (that is, until an nb_free() or an inet_queue() of course!) + */ +typedef struct netbuf { struct netbuf *m_nextpkt; } *netbuf_t; + +#ifdef KERNEL + +extern char *nb_map(netbuf_t nb); +extern netbuf_t nb_alloc(unsigned size); +extern netbuf_t nb_alloc_wrapper(void *data, unsigned size, + void freefunc(void *), void *freefunc_arg); + +extern void nb_free(netbuf_t nb); +extern void nb_free_wrapper(netbuf_t nb); +extern unsigned nb_size(netbuf_t nb); +extern int nb_read(netbuf_t nb, unsigned offset, unsigned size, void *target); +extern int nb_write(netbuf_t nb, unsigned offset, unsigned size, void *source); +extern int nb_shrink_top(netbuf_t nb, unsigned size); +extern int nb_grow_top(netbuf_t nb, unsigned size); +extern int nb_shrink_bot(netbuf_t nb, unsigned size); +extern int nb_grow_bot(netbuf_t nb, unsigned size); + +#endif /* KERNEL */ +#endif /* _NETBUF_ */ diff --git a/NeXT/ppp_tty.c b/NeXT/ppp_tty.c new file mode 100644 index 0000000..76aed49 --- /dev/null +++ b/NeXT/ppp_tty.c @@ -0,0 +1,1218 @@ +/* $ID: ppp_tty.c,v 1.4 1994/12/13 03:42:17 paulus Exp paulus $ */ + +/* + * ppp_tty.c - Point-to-Point Protocol (PPP) driver for asynchronous + * tty devices. + * + * Copyright (c) 1989 Carnegie Mellon University. + * All rights reserved. + * + * Redistribution and use in source and binary forms are permitted + * provided that the above copyright notice and this paragraph are + * duplicated in all such forms and that any documentation, + * advertising materials, and other materials related to such + * distribution and use acknowledge that the software was developed + * by Carnegie Mellon University. The name of the + * University may not be used to endorse or promote products derived + * from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED + * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. + * + * Drew D. Perkins + * Carnegie Mellon University + * 4910 Forbes Ave. + * Pittsburgh, PA 15213 + * (412) 268-8576 + * ddp@andrew.cmu.edu + * + * Based on: + * @(#)if_sl.c 7.6.1.2 (Berkeley) 2/15/89 + * + * Copyright (c) 1987 Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms are permitted + * provided that the above copyright notice and this paragraph are + * duplicated in all such forms and that any documentation, + * advertising materials, and other materials related to such + * distribution and use acknowledge that the software was developed + * by the University of California, Berkeley. The name of the + * University may not be used to endorse or promote products derived + * from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED + * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. + * + * Serial Line interface + * + * Rick Adams + * Center for Seismic Studies + * 1300 N 17th Street, Suite 1450 + * Arlington, Virginia 22209 + * (703)276-7900 + * rick@seismo.ARPA + * seismo!rick + * + * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris). + * Converted to 4.3BSD Beta by Chris Torek. + * Other changes made at Berkeley, based in part on code by Kirk Smith. + * + * Converted to 4.3BSD+ 386BSD by Brad Parker (brad@cayman.com) + * Added VJ tcp header compression; more unified ioctls + * + * Extensively modified by Paul Mackerras (paulus@cs.anu.edu.au). + * Cleaned up a lot of the mbuf-related code to fix bugs that + * caused system crashes and packet corruption. Changed pppstart + * so that it doesn't just give up with a collision if the whole + * packet doesn't fit in the output ring buffer. + * + * Added priority queueing for interactive IP packets, following + * the model of if_sl.c, plus hooks for bpf. + * Paul Mackerras (paulus@cs.anu.edu.au). + * + * Rewritten for NextStep's funky kernel functions, I/O threads, + * and netbufs (instead of real mbufs). Also, ifnets don't install + * into the kernel under NS as they do under BSD. We have tried to + * make the code remain as similar to the NetBSD version without + * incurring too much hassle. This code is the merge of + * Philip Prindeville's /Pete French's + * and Stephen Perkins' independent ports. + * + */ + +/* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */ +/* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:59 cgd Exp */ + +/* #include "ppp.h" */ +#if NUM_PPP > 0 + +#define KERNEL 1 +#define KERNEL_FEATURES 1 +#define INET 1 + +#include +#include +#include +#include "netbuf.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#include +/* NeXT broke spl.h in 3.2/m68k. Thanks EPS! */ + +#if defined(m68k) +#import "spl.h" +#else +#include +#import +#endif + +#include + +#include +#include + +#ifdef VJC +#include +#include +#include +#endif + +#include +#ifdef VJC +#include +#endif +#include +#include "if_pppvar.h" + +#include "inlines.h" + +int pppopen __P((dev_t dev, struct tty *tp)); +void pppclose __P((struct tty *tp)); +int pppread __P((struct tty *tp, struct uio *uio)); +int pppwrite __P((struct tty *tp, struct uio *uio)); +int ppptioctl __P((struct tty *tp, int cmd, void *data, int flag)); +void pppinput __P((int c, struct tty *tp)); +void pppstart __P((struct tty *tp)); + +netbuf_t pppgetbuf __P((netif_t)); +int pppoutput __P((netif_t ifp, netbuf_t m, void *arg)); +void pppintr __P((void *)); + +static u_int16_t pppfcs __P((u_int16_t fcs, u_char *cp, int len)); +static void pppasyncstart __P((struct ppp_softc *)); +static void pppasyncctlp __P((struct ppp_softc *)); +static void pppasyncrelinq __P((struct ppp_softc *)); +static int ppp_timeout __P((void *)); +void pppgetm __P((struct ppp_softc *sc)); +static void pppdumpb __P((u_char *b, int l)); +void ppplogchar __P((struct ppp_softc *, int)); + +extern kern_server_t instance; + +#ifdef ADD_ERRORS + +static int in_error_pkt_count = 0; /* Number of packets received */ +static int in_error_limit = -1; /* Insert error at this limit */ +static int in_error_max_sep = 50; /* Max range of random number */ + +static int out_error_pkt_count = 0; /* Number of packets sent */ +static int out_error_limit = -1; /* Insert error at this limit */ +static int out_error_max_sep = 50; /* Max range of random number */ + +#endif /* ADD_ERRORS */ + + +/* + * Does c need to be escaped? + */ +#define ESCAPE_P(c) (sc->sc_asyncmap[(c) >> 5] & (1 << ((c) & 0x1F))) + +#define CCOUNT(q) ((q)->c_cc) + +#define PPP_HIWAT 400 /* Don't start a new packet if HIWAT on que */ + +#include "linedisc.h" + + +extern int ttymodem(struct tty*, int); +extern int ttselect(struct tty *tp, int rw); + + +static netbuf_t +pppgetinbuf(netif_t ifp) +{ + register struct ppp_softc *sc = &ppp_softc[if_unit(ifp)]; + netbuf_t nb; + int len = MAX(sc->sc_mru, PPP_MTU) + sizeof (struct ifnet *) + +#ifdef VJC + VJ_HDRLEN + +#endif + PPP_HDRLEN + PPP_FCSLEN; + nb = ppp_nb_alloc(len); + if (nb != NULL) + { +#ifdef VJC + ppp_nb_shrink_top(nb, VJ_HDRLEN + PPP_HDRLEN); +#else + ppp_nb_shrink_top(nb, PPP_HDRLEN); +#endif + } + + return nb; +} + +/* + * I was a bit worried about reentrancy here. +++SJP + */ + +void +pppfillfreeq(void *arg) +{ + struct ppp_softc *sc = (struct ppp_softc *)arg; + netbuf_t nb; + volatile static int in = 0; + + if (in) + return; + in = 1; + + while(!nbq_high(&sc->sc_freeq)) { + nb = pppgetinbuf(sc->sc_if); + if (! nb) break; +#if 0 + /* + * we no longer reset the length to 0 and then advance the + * packet length bit by bit; instead we write into it whenever + * we want, and at the end resize the packet before handing-off. + */ + nb_shrink_bot(nb, nb_size(nb)); +#endif + nbq_enqueue(&sc->sc_freeq, nb); + } + + in = 0; +} + +/* + * Line specific open routine for async tty devices. + * Attach the given tty to the first available ppp unit. + */ +/* ARGSUSED */ +int +pppopen(dev, tp) + dev_t dev; + register struct tty *tp; +{ + struct proc *p = curproc; /* XXX */ + register struct ppp_softc *sc; + int s; + + if (! suser()) + return EPERM; + + if (tp->t_line == PPPDISC) { + sc = (struct ppp_softc *) tp->t_sc; + if (sc != NULL && sc->sc_devp == (void *) tp) + return (0); + } + + if ((sc = pppalloc(p->p_pid)) == NULL) + return ENXIO; + + if (sc->sc_relinq) + (*sc->sc_relinq)(sc); /* get previous owner to relinquish the unit */ + + pppfillfreeq((void *) sc); /* fill the free queue - we may block */ + + s = splimp(); + sc->sc_ilen = 0; + sc->sc_m = NULL; + bzero(sc->sc_asyncmap, sizeof(sc->sc_asyncmap)); + sc->sc_asyncmap[0] = 0xffffffff; + sc->sc_asyncmap[3] = 0x60000000; + sc->sc_rasyncmap = 0; + sc->sc_devp = (void *) tp; + sc->sc_start = pppasyncstart; + sc->sc_ctlp = pppasyncctlp; + sc->sc_relinq = pppasyncrelinq; + sc->sc_outm = NULL; + pppgetm(sc); + if_flags_set(sc->sc_if, if_flags(sc->sc_if) | IFF_RUNNING); + + tp->t_sc = (caddr_t) sc; + ttyflush(tp, FREAD | FWRITE); + splx(s); + + return (0); +} + +/* + * Line specific close routine. + * Detach the tty from the ppp unit. + * Mimics part of ttyclose(). + */ +void +pppclose(tp) + struct tty *tp; +{ + register struct ppp_softc *sc; + int s; + + ttywflush(tp); + s = splimp(); /* paranoid; splnet probably ok */ + tp->t_line = 0; + sc = (struct ppp_softc *) tp->t_sc; + if (sc != NULL) { + tp->t_sc = NULL; + if (tp == (struct tty *) sc->sc_devp) { + pppasyncrelinq(sc); + pppdealloc(sc); + } + } + splx(s); + return; +} + +/* + * Relinquish the interface unit to another device. + */ +static void +pppasyncrelinq(sc) + struct ppp_softc *sc; +{ + int s; + + s = splimp(); + if (sc->sc_outm) { + nb_free(sc->sc_outm); + sc->sc_outm = NULL; + } + if (sc->sc_m) { + nb_free(sc->sc_m); + sc->sc_m = NULL; + } + if (sc->sc_flags & SC_TIMEOUT) { + ns_untimeout(ppp_timeout, (void *) sc); + sc->sc_flags &= ~SC_TIMEOUT; + } + splx(s); +} + +/* + * Line specific (tty) read routine. + */ +int +pppread(tp, uio) + register struct tty *tp; + struct uio *uio; +{ + register struct ppp_softc *sc = (struct ppp_softc *)tp->t_sc; + netbuf_t m; + register int s; + int error = 0; + + if ((tp->t_state & TS_CARR_ON) == 0 && (tp->t_flags & CLOCAL) == 0) + return 0; /* end of file */ + if (sc == NULL || tp != (struct tty *) sc->sc_devp) + return 0; + s = splimp(); + while (nbq_empty(&sc->sc_inq) && tp->t_line == PPPDISC) { + if (tp->t_state & (TS_ASYNC | TS_NBIO)) { + splx(s); + return (EWOULDBLOCK); + } +#if 0 + /* NetBSD version... */ + error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI|PCATCH, ttyin, 0); + if (error) { + splx(s); + return error; + } +#else + sleep((caddr_t)&tp->t_rawq, TTIPRI); +#endif + } + if (tp->t_line != PPPDISC) { + splx(s); + return (-1); + } + + /* Pull place-holder byte out of canonical queue */ + getc(&tp->t_canq); + + /* Get the packet from the input queue */ + m = nbq_dequeue(&sc->sc_inq); + splx(s); + if (nbuf == NULL){ + if (sc->sc_flags & SC_DEBUG) + IOLogDbg("Read didn't get a buffer at %s %d\n", __FILE__, __LINE__); + return -1; + } + error = uiomove(nb_map(m), nb_size(m), UIO_READ, uio); + nb_free(m); + return (error); +} + +/* + * Line specific (tty) write routine. + */ +int +pppwrite(tp, uio) + register struct tty *tp; + struct uio *uio; +{ + register struct ppp_softc *sc = (struct ppp_softc *)tp->t_sc; + netbuf_t m; + struct sockaddr dst; + int len, error; + + if ((tp->t_state & TS_CARR_ON) == 0 && (tp->t_flags & CLOCAL) == 0) + return 0; /* wrote 0 bytes */ + if (tp->t_line != PPPDISC) + return (EINVAL); + if (sc == NULL || tp != (struct tty *) sc->sc_devp) + return EIO; + if (uio->uio_resid > if_mtu(sc->sc_if) + PPP_HDRLEN || + uio->uio_resid < PPP_HDRLEN) + return (EMSGSIZE); + m = pppgetbuf(sc->sc_if); + if (m == NULL){ + if (sc->sc_flags & SC_DEBUG) + IOLogDbg("No buffers available for user level write()\n"); + return(ENOBUFS); + } + ppp_nb_grow_top(m, PPP_HDRLEN); + len = uio->uio_resid; + if (error = uiomove(nb_map(m), nb_size(m), UIO_WRITE, uio)) { + nb_free(m); + return error; + } + nb_shrink_bot(m, nb_size(m) - len); + dst.sa_family = AF_UNSPEC; + bcopy(mtod(m, u_char *), dst.sa_data, PPP_HDRLEN); + ppp_nb_shrink_top(m, PPP_HDRLEN); + return (pppoutput(sc->sc_if, m, &dst)); +} + +/* + * Line specific (tty) ioctl routine. + * This discipline requires that tty device drivers call + * the line specific l_ioctl routine from their ioctl routines. + */ +/* ARGSUSED */ +int +ppptioctl(tp, cmd, data, flag) + struct tty *tp; + void *data; + int cmd, flag; +{ + struct ppp_softc *sc = (struct ppp_softc *) tp->t_sc; + int error, s; + + if (sc == NULL || tp != (struct tty *) sc->sc_devp) + return -1; + + error = 0; + switch (cmd) { + case PPPIOCSASYNCMAP: + if (! suser()) + return EPERM; +#if 0 + IOLogDbg("ppp%d: set async map to 0x%08x\n", if_unit(sc->sc_if), + *(u_long *)data); +#endif + sc->sc_asyncmap[0] = *(u_int *)data; + break; + + case PPPIOCGASYNCMAP: + *(u_int *)data = sc->sc_asyncmap[0]; +#if 0 + IOLogDbg("ppp%d: asyncmap gets 0x%x\n", if_unit(sc->sc_if), *(u_int *)data); +#endif + break; + + case PPPIOCSRASYNCMAP: + if (! suser()) + return EPERM; + sc->sc_rasyncmap = *(u_int *)data; +#if 0 + IOLogDbg("ppp%d: setting rasyncmap 0x%x\n", if_unit(sc->sc_if), sc->sc_rasyncmap); +#endif + break; + + case PPPIOCGRASYNCMAP: + *(u_int *)data = sc->sc_rasyncmap; +#if 0 + IOLogDbg("ppp%d: rasyncmap gets 0x%x\n", if_unit(sc->sc_if), *(u_int *)data); +#endif + break; + + case PPPIOCSXASYNCMAP: + if (! suser()) + return EPERM; + s = spltty(); + bcopy(data, sc->sc_asyncmap, sizeof(sc->sc_asyncmap)); + sc->sc_asyncmap[1] = 0; /* mustn't escape 0x20 - 0x3f */ + sc->sc_asyncmap[2] &= ~0x40000000; /* mustn't escape 0x5e */ + sc->sc_asyncmap[3] |= 0x60000000; /* must escape 0x7d, 0x7e */ + splx(s); + break; + + case PPPIOCGXASYNCMAP: + bcopy(sc->sc_asyncmap, data, sizeof(sc->sc_asyncmap)); +#if 0 + IOLogDbg("ppp%d: xasyncmap gets 0x%x/0x%x/0x%x/0x%x\n", if_unit(sc->sc_if), ((u_int *)data)[0], ((u_int *)data)[1], ((u_int *)data)[2], ((u_int *)data)[3]); +#endif + break; + + default: + error = pppioctl(sc, cmd, data, flag); + if (error == 0 && cmd == PPPIOCSMRU) + pppgetm(sc); + } + +#ifdef i386 + if (! error && (cmd & IOC_OUT)) { + struct uthread *_u = uthread_from_thread(current_thread()); + + /* third arg is destination in ioctl() call... */ + copyout(data, (caddr_t) _u->uu_arg[2], (cmd >> 16) & IOCPARM_MASK); + } +#endif + + return error; +} + +/* + * FCS lookup table as calculated by genfcstab. + */ +static const u_int16_t fcstab[256] = { + 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, + 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, + 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, + 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876, + 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd, + 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5, + 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c, + 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974, + 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb, + 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3, + 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a, + 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72, + 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9, + 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1, + 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738, + 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70, + 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7, + 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff, + 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036, + 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e, + 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5, + 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd, + 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134, + 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c, + 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3, + 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb, + 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232, + 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a, + 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1, + 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9, + 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, + 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78 +}; + +/* + * Calculate a new FCS given the current FCS and the new data. + */ +static u_int16_t +pppfcs(fcs, cp, len) + register u_int16_t fcs; + register u_char *cp; + register int len; +{ + while (len--) + fcs = PPP_FCS(fcs, *cp++); + return (fcs); +} + +/* + * This gets called from pppoutput when a new packet is + * put on a queue. + */ +static void +pppasyncstart(sc) + register struct ppp_softc *sc; +{ + register struct tty *tp = (struct tty *) sc->sc_devp; + int s; + + s = splimp(); + pppstart(tp); + splx(s); +} + +/* + * This gets called when a received packet is placed on + * the inq. + */ +static void +pppasyncctlp(sc) + struct ppp_softc *sc; +{ + struct tty *tp; + + /* Put a placeholder byte in canq for ttselect()/ttnread(). */ + tp = (struct tty *) sc->sc_devp; + putc(0, &tp->t_canq); + ttwakeup(tp); +} + +/* + * Start output on async tty interface. Get another datagram + * to send from the interface queue and start sending it. + */ +void +pppstart(tp) + register struct tty *tp; +{ + register struct ppp_softc *sc = (struct ppp_softc *) tp->t_sc; + register netbuf_t m; + register int len; + register u_char *start, *stop, *cp; + int n, ndone, done, idle; + + if ((tp->t_state & TS_CARR_ON) == 0 && (tp->t_flags & CLOCAL) == 0 + || sc == NULL || tp != (struct tty *) sc->sc_devp) { + if (tp->t_oproc != NULL) + (*tp->t_oproc)(tp); + return; + } + + idle = 0; +#ifdef OLD_MUX + while (CCOUNT(&tp->t_outq) == 0) { +#else + while (CCOUNT(&tp->t_outq) < PPP_HIWAT) { +#endif + /* + * See if we have an existing packet partly sent. + * If not, get a new packet and start sending it. + */ + m = sc->sc_outm; + if (m == NULL) { + /* + * Get another packet to be sent. + */ + m = ppp_dequeue(sc); + if (m == NULL) { + idle = 1; + break; + } + + /* + * The extra PPP_FLAG will start up a new packet, and thus + * will flush any accumulated garbage. We do this whenever + * the line may have been idle for some time. + */ + if (CCOUNT(&tp->t_outq) == 0) { + ++sc->sc_bytessent; + (void) putc(PPP_FLAG, &tp->t_outq); + } + + /* Calculate the FCS for the first netbuf's worth. */ + sc->sc_outfcs = pppfcs(PPP_INITFCS, mtod(m, u_char *), nb_size(m)); + sc->sc_outfcs ^= 0xffff; + +#ifdef ADD_ERRORS + /* + * This section will adds random errors to + * outgoing packets. + */ + + if (out_error_limit == -1) /* Initial time through */ + { + out_error_limit = rand() % out_error_max_sep; + IOLog("Introducing outgoing random error after %d more packets\n", + out_error_limit); + } + + if (out_error_pkt_count >= out_error_limit) + { + sc->sc_outfcs ^= 0x99; /* Munge with some noise */ + out_error_pkt_count = 0; + out_error_limit = rand() % out_error_max_sep; + IOLog("Introducing outgoing random error after %d more packets\n", + out_error_limit); + } + else + ++out_error_pkt_count; + + +#endif /* ADD_ERRORS */ + + cp = mtod(m, u_char *) + nb_size(m); + nb_grow_bot(m, PPP_FCSLEN); + *cp++ = sc->sc_outfcs & 0xFF; + *cp++ = (sc->sc_outfcs >> 8) & 0xFF; + } + + start = mtod(m, u_char *); + len = nb_size(m); + stop = start + len; + while (len > 0) { + /* + * Find out how many bytes in the string we can + * handle without doing something special. + */ + for (cp = start; cp < stop; cp++) + if (ESCAPE_P(*cp)) + break; + n = cp - start; + if (n) { + /* NetBSD (0.9 or later), 4.3-Reno or similar. */ + ndone = n - b_to_q(start, n, &tp->t_outq); + len -= ndone; + start += ndone; + sc->sc_bytessent += ndone; + + if (ndone < n) + break; /* packet doesn't fit */ + } + + /* + * If there are characters left in the netbuf, + * the first one must be special.. + * Put it out in a different form. + */ + if (len) { + if (putc(PPP_ESCAPE, &tp->t_outq)) + break; + if (putc(*start ^ PPP_TRANS, &tp->t_outq)) { + (void) unputc(&tp->t_outq); + break; + } + sc->sc_bytessent += 2; + start++; + len--; + } + } + /* + * If we didn't empty this netbuf, remember where we're up to. + */ + done = len == 0; + + if (!done) { + /* remember where we got to */ + ppp_nb_shrink_top(m, start - mtod(m, u_char *)); + break; /* can't do any more at the moment */ + } + + /* + * Output trailing PPP flag and finish packet. + * We make the length zero in case the flag + * cannot be output immediately. + */ + ppp_nb_shrink_top(m, nb_size(m)); + if (putc(PPP_FLAG, &tp->t_outq)) + break; + sc->sc_bytessent++; + + /* Finished with this netbuf; free it and move on. */ + nb_free(m); + m = NULL; + incr_cnt(sc->sc_if, if_opackets); + + sc->sc_outm = m; + } + + /* + * If there is stuff in the output queue, send it now. + * We are being called in lieu of ttstart and must do what it would. + */ + if (tp->t_oproc != NULL) + (*tp->t_oproc)(tp); + + /* + * This timeout is needed for operation on a pseudo-tty, + * because the pty code doesn't call pppstart after it has + * drained the t_outq. + */ + if (!idle && (sc->sc_flags & SC_TIMEOUT) == 0) { + ns_timeout(ppp_timeout, (void *) sc, 1 * (1000000000L / HZ), CALLOUT_PRI_SOFTINT0); + sc->sc_flags |= SC_TIMEOUT; + } + + return; +} + +/* + * Timeout routine - try to start some more output. + */ +static int +ppp_timeout(x) + void *x; +{ + struct ppp_softc *sc = (struct ppp_softc *) x; + struct tty *tp = (struct tty *) sc->sc_devp; + int s; + + s = splimp(); + sc->sc_flags &= ~SC_TIMEOUT; + pppstart(tp); + splx(s); + return 0; +} + +/* + * Allocate enough netbuf to handle current MRU. + * + * Warning Will Robinson: pppgetm() can get called at interrupt-level! + */ +void +pppgetm(sc) + register struct ppp_softc *sc; +{ + int s; + + s = splimp(); + /* + * When the MRU is being changed, we could conceivably end up + * nuking a packet being received, but I doubt it, since the + * hand-shake is lock-step (ie. single packet). + */ + if (sc->sc_m != NULL) + nb_free(sc->sc_m); + sc->sc_m = nbq_dequeue(&sc->sc_freeq); + splx(s); +} + +/* + * 4.3 says this is an unused function. However, + * it appears to be returning a NULL terminated string + * of several characters. My guess is that the serial + * driver is doing a little buffering so that we don't + * get burdend with interrupts. + * + * This function gets called when you use the NeXT + * supplied serial drivers. It does not get called + * with the MuX driver. + * + * In order to expedite the work done here, we + * handle most things here that don't require + * processing of a PPP_FLAG. + * + */ + +void +ppprend(cp, n, tp) + unsigned char *cp; + int n; + struct tty *tp; +{ + +#ifndef OPTIMIZE_PPPREND + while (n--) pppinput((u_char) *cp++, tp); +#else +#warning OPTIMIZE_PPPREND in effect + + register struct ppp_softc *sc = (struct ppp_softc *)tp->t_sc; + register int ret; + + if (sc == NULL || tp != (struct tty *) sc->sc_devp) + { + printf("Warning, bad softc structure at %s %d\n", __FILE__, __LINE__); + return; + } + + /* + * We can handle FLUSHs, ESCAPES, and non PPP_FLAG characters + */ + + while (n) + { + if (sc->sc_flags & SC_FLUSH) + { + do + { + if (*(cp++) == PPP_FLAG) + { + pppinput(PPP_FLAG, tp); + --n; + break; + } + else if (sc->sc_flags & SC_LOG_FLUSH) + ppplogchar(sc, *cp); + } + while(--n); + } + else if (sc->sc_ilen > 3 && + (nb_size(sc->sc_m) - sc->sc_ilen) > n && + *cp != PPP_FLAG && + *cp != PPP_ESCAPE) /* Dont really handle escapes properly...should */ + { + unsigned char* cp1 = cp; + if (sc->sc_flags & SC_ESCAPED) + { + sc->sc_flags &= ~SC_ESCAPED; + *cp ^= PPP_TRANS; + } + + do + { + sc->sc_fcs = PPP_FCS(sc->sc_fcs, *(cp++)); + if (sc->sc_flags & SC_LOG_RAWIN) + ppplogchar(sc, *cp); + + } while(--n && *cp != PPP_FLAG && *cp != PPP_ESCAPE); + + + bcopy(cp1, sc->sc_mp, (cp-cp1)); + + sc->sc_bytesrcvd += (cp - cp1); + sc->sc_ilen += (cp-cp1); + sc->sc_mp += (cp-cp1); + } + else + { + --n; + pppinput(*(cp++), tp); + } + } + +#endif /* OPTIMIZE_PPPREND */ +} + +/* + * tty interface receiver interrupt. + */ +static const unsigned paritytab[8] = { + 0x96696996, 0x69969669, 0x69969669, 0x96696996, + 0x69969669, 0x96696996, 0x96696996, 0x69969669 +}; + +void +pppinput(c, tp) + int c; + register struct tty *tp; +{ + register struct ppp_softc *sc; + netbuf_t m; + int ilen, s; + + sc = (struct ppp_softc *) tp->t_sc; + if (sc == NULL || tp != (struct tty *) sc->sc_devp) + return; + + ++tk_nin; + ++sc->sc_bytesrcvd; + + if (c & TTY_FE) { + /* framing error or overrun on this char - abort packet */ + IOLogDbg("ppp%d: bad char 0x%x\n", if_unit(sc->sc_if), c); + goto flush; + } + + c &= 0xff; + + if (c & 0x80) + sc->sc_flags |= SC_RCV_B7_1; + else + sc->sc_flags |= SC_RCV_B7_0; + if (paritytab[c >> 5] & (1 << (c & 0x1F))) + sc->sc_flags |= SC_RCV_ODDP; + else + sc->sc_flags |= SC_RCV_EVNP; + + if (sc->sc_flags & SC_LOG_RAWIN) + ppplogchar(sc, c); + + if (c == PPP_FLAG) { + + + ilen = sc->sc_ilen; + sc->sc_ilen = 0; + + if (sc->sc_rawin_count > 0) + ppplogchar(sc, -1); + +#ifdef ADD_ERRORS + /* + * This section will adds random packet + * errors if defined. + */ + + /* + * Initial time through + */ + if (in_error_limit == -1) + { + in_error_limit = rand() % in_error_max_sep; + IOLog("Introducing incoming random error after %d more packets\n", + in_error_limit); + } + + if ((in_error_pkt_count >= in_error_limit) && (ilen != 0)) + { + sc->sc_fcs = !PPP_GOODFCS; + in_error_pkt_count = 0; + in_error_limit = rand() % in_error_max_sep; + IOLog("Introducing incoming random error after %d more packets\n", + in_error_limit); + } + + if (ilen != 0) + ++in_error_pkt_count; + + +#endif /* ADD_ERRORS */ + + /* + * From the RFC: + * Each Control Escape octet is also + * removed, and the following octet is exclusive-or'd with hexadecimal + * 0x20, unless it is the Flag Sequence (which aborts a frame). + * + * So, if SC_ESCAPED is set, then we've seen the packet + * abort sequence "}~". + */ + + if (sc->sc_flags & (SC_FLUSH | SC_ESCAPED) + || ilen > 0 && sc->sc_fcs != PPP_GOODFCS) { + sc->sc_flags |= SC_PKTLOST; /* note the dropped packet */ + if ((sc->sc_flags & (SC_FLUSH | SC_ESCAPED)) == 0){ + IOLogDbg("ppp%d: bad fcs 0x%04x\n", if_unit(sc->sc_if), sc->sc_fcs); + incr_cnt(sc->sc_if, if_ierrors); + } else + sc->sc_flags &= ~(SC_FLUSH | SC_ESCAPED); + return; + } + + if (ilen < PPP_HDRLEN + PPP_FCSLEN) { + if (ilen) { + IOLogDbg("ppp%d: too short (%d)\n", if_unit(sc->sc_if), ilen); + incr_cnt(sc->sc_if, if_ierrors); + sc->sc_flags |= SC_PKTLOST; + } + return; + } + + /* + * Remove FCS trailer. Set packet length... + */ + ilen -= PPP_FCSLEN; + nb_shrink_bot(sc->sc_m, nb_size(sc->sc_m) - ilen); + + /* excise this netbuf */ + m = sc->sc_m; + sc->sc_m = NULL; + + ppppktin(sc, m, sc->sc_flags & SC_PKTLOST); + sc->sc_flags &= ~SC_PKTLOST; + + pppgetm(sc); + return; + } + + if (sc->sc_flags & SC_FLUSH) { + if (sc->sc_flags & SC_LOG_FLUSH) + ppplogchar(sc, c); + return; + } + +/* + * From the RFC: + * On reception, prior to FCS computation, each octet with value less + * than hexadecimal 0x20 is checked. If it is flagged in the receiving + * ACCM, it is simply removed (it may have been inserted by intervening + * data communications equipment). Each Control Escape octet is also + * removed, and the following octet is exclusive-or'd with hexadecimal + * 0x20, unless it is the Flag Sequence (which aborts a frame). + */ + + if (c < 0x20 && (sc->sc_rasyncmap & (1 << c))) { + return; + } + + if (sc->sc_flags & SC_ESCAPED) { + sc->sc_flags &= ~SC_ESCAPED; + c ^= PPP_TRANS; + } else if (c == PPP_ESCAPE) { + sc->sc_flags |= SC_ESCAPED; +/* splx(s); */ + return; + } + + /* + * Initialize buffer on first octet received. + * First octet could be address or protocol (when compressing + * address/control). + * Second octet is control. + * Third octet is first or second (when compressing protocol) + * octet of protocol. + * Fourth octet is second octet of protocol. + */ + if (sc->sc_ilen == 0) { + /* reset the input netbuf */ + if (sc->sc_m == NULL) { + pppgetm(sc); + if (sc->sc_m == NULL) { + /* + * We schedule a call here as pppindrain will + * not get scheduled and we need the free buffers + */ + IOLog("ppp%d: no input netbufs!\n", if_unit(sc->sc_if)); + (void)pppsched(pppfillfreeq, sc); + goto flush; + } + } + m = sc->sc_m; + sc->sc_mp = mtod(m, char *); + sc->sc_fcs = PPP_INITFCS; + if (c != PPP_ALLSTATIONS) { + if (sc->sc_flags & SC_REJ_COMP_AC) { + IOLogDbg("ppp%d: garbage received: 0x%02x (need 0x%02x)\n", + if_unit(sc->sc_if), c, PPP_ALLSTATIONS); + goto flush; + } + *sc->sc_mp++ = PPP_ALLSTATIONS; + *sc->sc_mp++ = PPP_UI; + sc->sc_ilen += 2; + } + } + if (sc->sc_ilen == 1 && c != PPP_UI) { + IOLogDbg("ppp%d: missing UI (0x%02x), got 0x%02x\n", + if_unit(sc->sc_if), PPP_UI, c); + goto flush; + } + if (sc->sc_ilen == 2 && (c & 1) == 1) { + /* a compressed protocol */ + *sc->sc_mp++ = 0; + sc->sc_ilen++; + } + if (sc->sc_ilen == 3 && (c & 1) == 0) { + IOLogDbg("ppp%d: bad protocol %x\n", if_unit(sc->sc_if), + (sc->sc_mp[-1] << 8) + c); + goto flush; + } + + /* packet beyond configured mru? */ + if (++sc->sc_ilen > sc->sc_mru + PPP_HDRLEN + PPP_FCSLEN) { + IOLogDbg("ppp%d: packet too big (%d bytes)\n", if_unit(sc->sc_if), + sc->sc_ilen); + goto flush; + } + + /* ilen was incremented above... */ + *sc->sc_mp++ = c; + sc->sc_fcs = PPP_FCS(sc->sc_fcs, c); + return; + + flush: + if (!(sc->sc_flags & SC_FLUSH)) { + incr_cnt(sc->sc_if, if_ierrors); + sc->sc_flags |= SC_FLUSH; + if (sc->sc_flags & SC_LOG_FLUSH) + ppplogchar(sc, c); + } + return; +} + +int +install_ppp_ld(void) +{ + return tty_ld_install(PPPDISC, NORMAL_LDISC, pppopen, + pppclose, pppread, pppwrite, ppptioctl, + pppinput, ppprend, pppstart, ttymodem, + ttselect); +} + +#define MAX_DUMP_BYTES 128 + +void +ppplogchar(sc, c) + struct ppp_softc *sc; + int c; +{ + if (c >= 0) + sc->sc_rawin[sc->sc_rawin_count++] = c; + if (sc->sc_rawin_count >= sizeof(sc->sc_rawin) + || c < 0 && sc->sc_rawin_count > 0) { + IOLog("ppp%d input:\n", if_unit(sc->sc_if)); + pppdumpb(sc->sc_rawin, sc->sc_rawin_count); + sc->sc_rawin_count = 0; + } +} + +static void +pppdumpb(b, l) + u_char *b; + int l; +{ + char buf[3*MAX_DUMP_BYTES+4]; + char *bp = buf; + static char digits[] = "0123456789abcdef"; + + while (l--) { + if (bp >= buf + sizeof(buf) - 3) { + *bp++ = '>'; + break; + } + *bp++ = digits[*b >> 4]; /* convert byte to ascii hex */ + *bp++ = digits[*b++ & 0xf]; + *bp++ = ' '; + } + + *bp = 0; + IOLog("%s\n", buf); +} +#endif /* NUM_PPP > 0 */ diff --git a/NeXT/random.c b/NeXT/random.c new file mode 100644 index 0000000..1c44fa1 --- /dev/null +++ b/NeXT/random.c @@ -0,0 +1,52 @@ +/* + * Because the standard library random number + * functions are not availble at the kernel level + * I wrote this simple random number generator. + * + * It uses the multiplicative congruential method. + * See pg 263 of Banks and Carson "Discrete-Event + * System Simulation". + * + */ + +#include "random.h" + +static unsigned x0=123457; /* seed */ +static unsigned a=16807; /* constant multiplier */ +static unsigned c=0; /* increment */ +static unsigned m=2147483647; /* modulus */ + +/* + * Set the seed to the argument. + */ + +void srand(unsigned i) +{ + x0 = i; +} + + +/* + * Use Linear Congruential Method to Generate + * sequence. Return either int or float... + */ + +unsigned rand(void) +{ + unsigned tmpseed; + + tmpseed = (a*x0+c) % m; + x0 = tmpseed; + return (unsigned) x0; +} + + + +float frand(void) +{ + unsigned tmpseed; + + tmpseed = (a*x0+c) % m; + x0 = tmpseed; + return (x0/(float)m); +} diff --git a/NeXT/random.h b/NeXT/random.h new file mode 100644 index 0000000..787368d --- /dev/null +++ b/NeXT/random.h @@ -0,0 +1,14 @@ +/* + * Because the standard library random number + * functions are not availble at the kernel level + * I wrote this simple random number generator. + * + * It uses the multiplicative congruential method. + * See pg 263 of Banks and Carson "Discrete-Event + * System Simulation". + * + */ + +void srand(unsigned i); +unsigned rand(void); +float frand(void); diff --git a/NeXT/spl.h b/NeXT/spl.h new file mode 100644 index 0000000..120f480 --- /dev/null +++ b/NeXT/spl.h @@ -0,0 +1,111 @@ +/* + * File: spl.h + * Author: Avadis Tevanian, Jr. + * + * Define inline macros for spl routines. + * + * HISTORY + * + * 14-May-90 Gregg Kellogg (gk) at NeXT + * Changed SPLCLOCK from 6 to 3, as much scheduling code expects + * splclock() == splsched(). Added splusclock(). + * + * 19-Jun-89 Mike DeMoney (mike) at NeXT + * Modified to allow spl assertions in spl_measured.h + */ + +#ifndef _KERNSERV_M68K_SPL_H_ +#define _KERNSERV_M68K_SPL_H_ + +#ifdef KERNEL_BUILD +#import +#else KERNEL_BUILD +/* #import */ +#endif KERNEL_BUILD + +#import + +#if NIPLMEAS && !defined(NO_IPLMEAS) +#import +#endif NIPLMEAS && !defined(NO_IPLMEAS) + +#ifndef SPLU_MACRO + +#ifdef ASSEMBLER +#define SPLU_MACRO(ipl) \ + movw sr,d0; \ + movw \#((ipl)*256 + 0x2000),sr; + +#define splx(nsr) \ + movw sr,d0; \ + movw nsr,sr; + +#else ASSEMBLER + +#define SPLU_MACRO(x) \ +({ register short ret; \ + asm volatile ("movw sr,%0" : "=dm" (ret)); \ + asm volatile ("movw %1,sr" : "=m" (*(char *)0): "Jdm" ((short)(x)*256+0x2000)); \ + ret; \ +}) + +#define splx(x) \ +({ register short ret; \ + asm volatile ("movw sr,%0" : "=dm" (ret)); \ + asm volatile ("movw %1,sr" : "=m" (*(char *)0): "Jdm" ((short)x)); \ + ret; \ +}) + +#endif ASSEMBLER + +#define SPLD_MACRO(ipl) SPLU_MACRO(ipl) +#define spln(x) splx(x) + +#endif SPLU_MACRO + +#define ipltospl(ipl) (SR_SUPER | ((ipl) << 8)) + +/* + * Define spls as the usual numbers (which should never be used + * directly. + */ + +#define spl0() SPLD_MACRO(0) +#define spl1() SPLU_MACRO(1) +#define spl2() SPLU_MACRO(2) +#define spl3() SPLU_MACRO(3) +#define spl4() SPLU_MACRO(4) +#define spl5() SPLU_MACRO(5) +#define spl6() SPLU_MACRO(6) +#define spl7() SPLU_MACRO(7) + +/* + * Define spl mnemonics. + */ +#define IPLHIGH 7 +#define IPLDMA 6 +#define IPLUSCLOCK 6 +#define IPLSCC 5 +#define IPLCLOCK 3 +#define IPLBIO 3 +#define IPLSCHED 3 +#define IPLIMP 3 +#define IPLVM 3 +#define IPLNET 2 +#define IPLTTY 1 +#define IPLSOFTCLOCK 1 + +#define splhigh() SPLU_MACRO(IPLHIGH) +#define splusclock() SPLU_MACRO(IPLUSCLOCK) +#define spldma() SPLU_MACRO(IPLDMA) +#define splscc() SPLU_MACRO(IPLSCC) +#define splclock() SPLU_MACRO(IPLCLOCK) +#define splbio() SPLU_MACRO(IPLBIO) +#define splsched() SPLU_MACRO(IPLSCHED) +#define splimp() SPLU_MACRO(IPLIMP) +#define splvm() SPLU_MACRO(IPLVM) +#define splnet() SPLU_MACRO(IPLNET) +#define spltty() SPLU_MACRO(IPLTTY) +#define splsoftclock() SPLU_MACRO(IPLSOFTCLOCK) + +#endif _KERNSERV_M68K_SPL_H_ diff --git a/NeXT/vjcompress.c b/NeXT/vjcompress.c new file mode 100644 index 0000000..237b1ff --- /dev/null +++ b/NeXT/vjcompress.c @@ -0,0 +1,584 @@ +/* + * Routines to compress and uncompess tcp packets (for transmission + * over low speed serial lines. + * + * Copyright (c) 1989 Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms are permitted + * provided that the above copyright notice and this paragraph are + * duplicated in all such forms and that any documentation, + * advertising materials, and other materials related to such + * distribution and use acknowledge that the software was developed + * by the University of California, Berkeley. The name of the + * University may not be used to endorse or promote products derived + * from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED + * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. + * + * Van Jacobson (van@helios.ee.lbl.gov), Dec 31, 1989: + * - Initial distribution. + * + * Modified June 1993 by Paul Mackerras, paulus@cs.anu.edu.au, + * so that the entire packet being decompressed doesn't have + * to be in contiguous memory (just the compressed header). + */ + +/* + * This version is used under SunOS 4.x, DEC Alpha OSF/1, AIX 4.x, + * and SVR4 systems including Solaris 2. + * + * $Id: vjcompress.c,v 1.1 1995/12/18 03:30:20 paulus Exp $ + */ + +#include +#include + +#ifdef __svr4__ +#ifndef __GNUC__ +#include /* for ntohl, etc. */ +#else +/* make sure we don't get the gnu "fixed" one! */ +#include "/usr/include/sys/byteorder.h" +#endif +#endif + +#ifdef __osf__ +#include +#endif +#include + +#ifdef __aix4__ +#define _NETINET_IN_SYSTM_H_ +typedef u_long n_long; +#else +#include +#endif + +#include +#include + +#include +#include + +#ifndef VJ_NO_STATS +#define INCR(counter) ++comp->stats.counter +#else +#define INCR(counter) +#endif + +#define BCMP(p1, p2, n) bcmp((char *)(p1), (char *)(p2), (int)(n)) +#undef BCOPY +#define BCOPY(p1, p2, n) bcopy((char *)(p1), (char *)(p2), (int)(n)) +#ifndef KERNEL +#define ovbcopy bcopy +#endif + +#ifdef __osf__ +#define getip_hl(base) (((base).ip_vhl)&0xf) +#define getth_off(base) ((((base).th_xoff)&0xf0)>>4) + +#else +#define getip_hl(base) ((base).ip_hl) +#define getth_off(base) ((base).th_off) +#endif + +void +vj_compress_init(comp, max_state) + struct vjcompress *comp; + int max_state; +{ + register u_int i; + register struct cstate *tstate = comp->tstate; + + if (max_state == -1) + max_state = MAX_STATES - 1; + bzero((char *)comp, sizeof(*comp)); + for (i = max_state; i > 0; --i) { + tstate[i].cs_id = i; + tstate[i].cs_next = &tstate[i - 1]; + } + tstate[0].cs_next = &tstate[max_state]; + tstate[0].cs_id = 0; + comp->last_cs = &tstate[0]; + comp->last_recv = 255; + comp->last_xmit = 255; + comp->flags = VJF_TOSS; +} + + +/* ENCODE encodes a number that is known to be non-zero. ENCODEZ + * checks for zero (since zero has to be encoded in the long, 3 byte + * form). + */ +#define ENCODE(n) { \ + if ((u_short)(n) >= 256) { \ + *cp++ = 0; \ + cp[1] = (n); \ + cp[0] = (n) >> 8; \ + cp += 2; \ + } else { \ + *cp++ = (n); \ + } \ +} +#define ENCODEZ(n) { \ + if ((u_short)(n) >= 256 || (u_short)(n) == 0) { \ + *cp++ = 0; \ + cp[1] = (n); \ + cp[0] = (n) >> 8; \ + cp += 2; \ + } else { \ + *cp++ = (n); \ + } \ +} + +#define DECODEL(f) { \ + if (*cp == 0) {\ + u_int32_t tmp = ntohl(f) + ((cp[1] << 8) | cp[2]); \ + (f) = htonl(tmp); \ + cp += 3; \ + } else { \ + u_int32_t tmp = ntohl(f) + (u_int32_t)*cp++; \ + (f) = htonl(tmp); \ + } \ +} + +#define DECODES(f) { \ + if (*cp == 0) {\ + u_short tmp = ntohs(f) + ((cp[1] << 8) | cp[2]); \ + (f) = htons(tmp); \ + cp += 3; \ + } else { \ + u_short tmp = ntohs(f) + (u_int32_t)*cp++; \ + (f) = htons(tmp); \ + } \ +} + +#define DECODEU(f) { \ + if (*cp == 0) {\ + (f) = htons((cp[1] << 8) | cp[2]); \ + cp += 3; \ + } else { \ + (f) = htons((u_int32_t)*cp++); \ + } \ +} + +u_int +vj_compress_tcp(ip, mlen, comp, compress_cid, vjhdrp) + register struct ip *ip; + u_int mlen; + struct vjcompress *comp; + int compress_cid; + u_char **vjhdrp; +{ + register struct cstate *cs = comp->last_cs->cs_next; + register u_int hlen = getip_hl(*ip); + register struct tcphdr *oth; + register struct tcphdr *th; + register u_int deltaS, deltaA; + register u_int changes = 0; + u_char new_seq[16]; + register u_char *cp = new_seq; + + /* + * Bail if this is an IP fragment or if the TCP packet isn't + * `compressible' (i.e., ACK isn't set or some other control bit is + * set). (We assume that the caller has already made sure the + * packet is IP proto TCP). + */ + if ((ip->ip_off & htons(0x3fff)) || mlen < 40) + return (TYPE_IP); + + th = (struct tcphdr *)&((int *)ip)[hlen]; + if ((th->th_flags & (TH_SYN|TH_FIN|TH_RST|TH_ACK)) != TH_ACK) + return (TYPE_IP); + /* + * Packet is compressible -- we're going to send either a + * COMPRESSED_TCP or UNCOMPRESSED_TCP packet. Either way we need + * to locate (or create) the connection state. Special case the + * most recently used connection since it's most likely to be used + * again & we don't have to do any reordering if it's used. + */ + INCR(vjs_packets); + if (ip->ip_src.s_addr != cs->cs_ip.ip_src.s_addr || + ip->ip_dst.s_addr != cs->cs_ip.ip_dst.s_addr || + *(int *)th != ((int *)&cs->cs_ip)[getip_hl(cs->cs_ip)]) { + /* + * Wasn't the first -- search for it. + * + * States are kept in a circularly linked list with + * last_cs pointing to the end of the list. The + * list is kept in lru order by moving a state to the + * head of the list whenever it is referenced. Since + * the list is short and, empirically, the connection + * we want is almost always near the front, we locate + * states via linear search. If we don't find a state + * for the datagram, the oldest state is (re-)used. + */ + register struct cstate *lcs; + register struct cstate *lastcs = comp->last_cs; + + do { + lcs = cs; cs = cs->cs_next; + INCR(vjs_searches); + if (ip->ip_src.s_addr == cs->cs_ip.ip_src.s_addr + && ip->ip_dst.s_addr == cs->cs_ip.ip_dst.s_addr + && *(int *)th == ((int *)&cs->cs_ip)[getip_hl(cs->cs_ip)]) + goto found; + } while (cs != lastcs); + + /* + * Didn't find it -- re-use oldest cstate. Send an + * uncompressed packet that tells the other side what + * connection number we're using for this conversation. + * Note that since the state list is circular, the oldest + * state points to the newest and we only need to set + * last_cs to update the lru linkage. + */ + INCR(vjs_misses); + comp->last_cs = lcs; + hlen += getth_off(*th); + hlen <<= 2; + if (hlen > mlen) + return (TYPE_IP); + goto uncompressed; + + found: + /* + * Found it -- move to the front on the connection list. + */ + if (cs == lastcs) + comp->last_cs = lcs; + else { + lcs->cs_next = cs->cs_next; + cs->cs_next = lastcs->cs_next; + lastcs->cs_next = cs; + } + } + + /* + * Make sure that only what we expect to change changed. The first + * line of the `if' checks the IP protocol version, header length & + * type of service. The 2nd line checks the "Don't fragment" bit. + * The 3rd line checks the time-to-live and protocol (the protocol + * check is unnecessary but costless). The 4th line checks the TCP + * header length. The 5th line checks IP options, if any. The 6th + * line checks TCP options, if any. If any of these things are + * different between the previous & current datagram, we send the + * current datagram `uncompressed'. + */ + oth = (struct tcphdr *)&((int *)&cs->cs_ip)[hlen]; + deltaS = hlen; + hlen += getth_off(*th); + hlen <<= 2; + if (hlen > mlen) + return (TYPE_IP); + + if (((u_short *)ip)[0] != ((u_short *)&cs->cs_ip)[0] || + ((u_short *)ip)[3] != ((u_short *)&cs->cs_ip)[3] || + ((u_short *)ip)[4] != ((u_short *)&cs->cs_ip)[4] || + getth_off(*th) != getth_off(*oth) || + (deltaS > 5 && BCMP(ip + 1, &cs->cs_ip + 1, (deltaS - 5) << 2)) || + (getth_off(*th) > 5 && BCMP(th + 1, oth + 1, (getth_off(*th) - 5) << 2))) + goto uncompressed; + + /* + * Figure out which of the changing fields changed. The + * receiver expects changes in the order: urgent, window, + * ack, seq (the order minimizes the number of temporaries + * needed in this section of code). + */ + if (th->th_flags & TH_URG) { + deltaS = ntohs(th->th_urp); + ENCODEZ(deltaS); + changes |= NEW_U; + } else if (th->th_urp != oth->th_urp) + /* argh! URG not set but urp changed -- a sensible + * implementation should never do this but RFC793 + * doesn't prohibit the change so we have to deal + * with it. */ + goto uncompressed; + + if (deltaS = (u_short)(ntohs(th->th_win) - ntohs(oth->th_win))) { + ENCODE(deltaS); + changes |= NEW_W; + } + + if (deltaA = ntohl(th->th_ack) - ntohl(oth->th_ack)) { + if (deltaA > 0xffff) + goto uncompressed; + ENCODE(deltaA); + changes |= NEW_A; + } + + if (deltaS = ntohl(th->th_seq) - ntohl(oth->th_seq)) { + if (deltaS > 0xffff) + goto uncompressed; + ENCODE(deltaS); + changes |= NEW_S; + } + + switch(changes) { + + case 0: + /* + * Nothing changed. If this packet contains data and the + * last one didn't, this is probably a data packet following + * an ack (normal on an interactive connection) and we send + * it compressed. Otherwise it's probably a retransmit, + * retransmitted ack or window probe. Send it uncompressed + * in case the other side missed the compressed version. + */ + if (ip->ip_len != cs->cs_ip.ip_len && + ntohs(cs->cs_ip.ip_len) == hlen) + break; + + /* (fall through) */ + + case SPECIAL_I: + case SPECIAL_D: + /* + * actual changes match one of our special case encodings -- + * send packet uncompressed. + */ + goto uncompressed; + + case NEW_S|NEW_A: + if (deltaS == deltaA && deltaS == ntohs(cs->cs_ip.ip_len) - hlen) { + /* special case for echoed terminal traffic */ + changes = SPECIAL_I; + cp = new_seq; + } + break; + + case NEW_S: + if (deltaS == ntohs(cs->cs_ip.ip_len) - hlen) { + /* special case for data xfer */ + changes = SPECIAL_D; + cp = new_seq; + } + break; + } + + deltaS = ntohs(ip->ip_id) - ntohs(cs->cs_ip.ip_id); + if (deltaS != 1) { + ENCODEZ(deltaS); + changes |= NEW_I; + } + if (th->th_flags & TH_PUSH) + changes |= TCP_PUSH_BIT; + /* + * Grab the cksum before we overwrite it below. Then update our + * state with this packet's header. + */ + deltaA = ntohs(th->th_sum); + BCOPY(ip, &cs->cs_ip, hlen); + + /* + * We want to use the original packet as our compressed packet. + * (cp - new_seq) is the number of bytes we need for compressed + * sequence numbers. In addition we need one byte for the change + * mask, one for the connection id and two for the tcp checksum. + * So, (cp - new_seq) + 4 bytes of header are needed. hlen is how + * many bytes of the original packet to toss so subtract the two to + * get the new packet size. + */ + deltaS = cp - new_seq; + cp = (u_char *)ip; + if (compress_cid == 0 || comp->last_xmit != cs->cs_id) { + comp->last_xmit = cs->cs_id; + hlen -= deltaS + 4; + *vjhdrp = (cp += hlen); + *cp++ = changes | NEW_C; + *cp++ = cs->cs_id; + } else { + hlen -= deltaS + 3; + *vjhdrp = (cp += hlen); + *cp++ = changes; + } + *cp++ = deltaA >> 8; + *cp++ = deltaA; + BCOPY(new_seq, cp, deltaS); + INCR(vjs_compressed); + return (TYPE_COMPRESSED_TCP); + + /* + * Update connection state cs & send uncompressed packet (that is, + * a regular ip/tcp packet but with the 'conversation id' we hope + * to use on future compressed packets in the protocol field). + */ + uncompressed: + BCOPY(ip, &cs->cs_ip, hlen); + ip->ip_p = cs->cs_id; + comp->last_xmit = cs->cs_id; + return (TYPE_UNCOMPRESSED_TCP); +} + +/* + * Called when we may have missed a packet. + */ +void +vj_uncompress_err(comp) + struct vjcompress *comp; +{ + comp->flags |= VJF_TOSS; + INCR(vjs_errorin); +} + +/* + * "Uncompress" a packet of type TYPE_UNCOMPRESSED_TCP. + */ +int +vj_uncompress_uncomp(buf, comp) + u_char *buf; + struct vjcompress *comp; +{ + register u_int hlen; + register struct cstate *cs; + register struct ip *ip; + + ip = (struct ip *) buf; + if (ip->ip_p >= MAX_STATES) { + comp->flags |= VJF_TOSS; + INCR(vjs_errorin); + return (0); + } + cs = &comp->rstate[comp->last_recv = ip->ip_p]; + comp->flags &=~ VJF_TOSS; + ip->ip_p = IPPROTO_TCP; + hlen = getip_hl(*ip); + hlen += getth_off(*((struct tcphdr *)&((int *)ip)[hlen])); + hlen <<= 2; + BCOPY(ip, &cs->cs_ip, hlen); + cs->cs_hlen = hlen; + INCR(vjs_uncompressedin); + return (1); +} + +/* + * Uncompress a packet of type TYPE_COMPRESSED_TCP. + * The packet starts at buf and is of total length total_len. + * The first buflen bytes are at buf; this must include the entire + * compressed TCP/IP header. This procedure returns the length + * of the VJ header, with a pointer to the uncompressed IP header + * in *hdrp and its length in *hlenp. + */ +int +vj_uncompress_tcp(buf, buflen, total_len, comp, hdrp, hlenp) + u_char *buf; + int buflen, total_len; + struct vjcompress *comp; + u_char **hdrp; + u_int *hlenp; +{ + register u_char *cp; + register u_int hlen, changes; + register struct tcphdr *th; + register struct cstate *cs; + register u_short *bp; + register u_int vjlen; + register u_int32_t tmp; + + INCR(vjs_compressedin); + cp = buf; + changes = *cp++; + if (changes & NEW_C) { + /* Make sure the state index is in range, then grab the state. + * If we have a good state index, clear the 'discard' flag. */ + if (*cp >= MAX_STATES) + goto bad; + + comp->flags &=~ VJF_TOSS; + comp->last_recv = *cp++; + } else { + /* this packet has an implicit state index. If we've + * had a line error since the last time we got an + * explicit state index, we have to toss the packet. */ + if (comp->flags & VJF_TOSS) { + INCR(vjs_tossed); + return (-1); + } + } + cs = &comp->rstate[comp->last_recv]; + hlen = getip_hl(cs->cs_ip) << 2; + th = (struct tcphdr *)&((u_char *)&cs->cs_ip)[hlen]; + th->th_sum = htons((*cp << 8) | cp[1]); + cp += 2; + if (changes & TCP_PUSH_BIT) + th->th_flags |= TH_PUSH; + else + th->th_flags &=~ TH_PUSH; + + switch (changes & SPECIALS_MASK) { + case SPECIAL_I: + { + register u_int32_t i = ntohs(cs->cs_ip.ip_len) - cs->cs_hlen; + /* some compilers can't nest inline assembler.. */ + tmp = ntohl(th->th_ack) + i; + th->th_ack = htonl(tmp); + tmp = ntohl(th->th_seq) + i; + th->th_seq = htonl(tmp); + } + break; + + case SPECIAL_D: + /* some compilers can't nest inline assembler.. */ + tmp = ntohl(th->th_seq) + ntohs(cs->cs_ip.ip_len) - cs->cs_hlen; + th->th_seq = htonl(tmp); + break; + + default: + if (changes & NEW_U) { + th->th_flags |= TH_URG; + DECODEU(th->th_urp); + } else + th->th_flags &=~ TH_URG; + if (changes & NEW_W) + DECODES(th->th_win); + if (changes & NEW_A) + DECODEL(th->th_ack); + if (changes & NEW_S) + DECODEL(th->th_seq); + break; + } + if (changes & NEW_I) { + DECODES(cs->cs_ip.ip_id); + } else { + cs->cs_ip.ip_id = ntohs(cs->cs_ip.ip_id) + 1; + cs->cs_ip.ip_id = htons(cs->cs_ip.ip_id); + } + + /* + * At this point, cp points to the first byte of data in the + * packet. Fill in the IP total length and update the IP + * header checksum. + */ + vjlen = cp - buf; + buflen -= vjlen; + if (buflen < 0) + /* we must have dropped some characters (crc should detect + * this but the old slip framing won't) */ + goto bad; + + total_len += cs->cs_hlen - vjlen; + cs->cs_ip.ip_len = htons(total_len); + + /* recompute the ip header checksum */ + bp = (u_short *) &cs->cs_ip; + cs->cs_ip.ip_sum = 0; + for (changes = 0; hlen > 0; hlen -= 2) + changes += *bp++; + changes = (changes & 0xffff) + (changes >> 16); + changes = (changes & 0xffff) + (changes >> 16); + cs->cs_ip.ip_sum = ~ changes; + + *hdrp = (u_char *) &cs->cs_ip; + *hlenp = cs->cs_hlen; + return vjlen; + + bad: + comp->flags |= VJF_TOSS; + INCR(vjs_errorin); + return (-1); +} diff --git a/README.NeXT b/README.NeXT index fe1ec2b..d13cab1 100644 --- a/README.NeXT +++ b/README.NeXT @@ -1,5 +1,5 @@ # -# $Id: README.NeXT,v 1.1 1995/08/10 06:48:23 paulus Exp $ +# $Id: README.NeXT,v 1.2 1995/12/18 03:29:36 paulus Exp $ # This distribution contains a port of PPP-2.2 for NeXT. The full @@ -27,6 +27,30 @@ See the file ./SETUP for general setup information for all computer systems. See the file ./NeXT/INSTALL for NeXT Specific installation instructions. See the file ./NeXT/README.NeXT for bugs. +New to release 0.4.6 (LKS version 4.14) +======================================= + + * Added a kernel optimization that will reduce the + interrupt load on the CPU. This optimization will + only work if you use the latest serial drivers supplied + by NeXT. You can get these drivers off of NeXTAnswers. + The optimization will not work if you use the Mux serial + driver. I believe that this optimization will solve many + of the kernel panics that people have reported. + + * Added support for Microsoft's extended DNS negotiation. + This will only be of use for people who server PPP clients + using Win95, TechSmith Corporation's Foray PPP client + (http://www.techsmith.com) for WFWG 3.11, or others whose + client supports this mechanism. Thanks to + clameter@hur.fuller.edu (Christoph Lameter) for supplying the + patches. This patch is totally untested since I don't serve + any clients, and if I did I wouldn't admit to running a + MicroSloth operating system. + + * Various minor fixes to documentation and examples. + + New to release 0.4.5 (LKS version 4.9 release1) ====================================== diff --git a/pppd/Makefile.NeXT b/pppd/Makefile.NeXT index 7e8ab81..8ded259 100644 --- a/pppd/Makefile.NeXT +++ b/pppd/Makefile.NeXT @@ -2,11 +2,13 @@ # pppd makefile for NeXT # # $Orignial: Makefile.ultrix,v 1.4 1994/09/01 00:40:40 paulus Exp $ -# $Id: Makefile.NeXT,v 1.1 1995/08/10 06:48:33 paulus Exp $ +# $Id: Makefile.NeXT,v 1.2 1995/12/18 03:30:45 paulus Exp $ # -ARCHFLAGS = -arch i386 -arch m68k -arch hppa -arch sparc +#ARCHFLAGS = -arch i386 -arch m68k -arch hppa -arch sparc #ARCHFLAGS = -arch i386 -arch m68k +ARCHFLAGS = + BINDIR = /usr/local/ppp/bin MANDIR = /usr/local/ppp/man @@ -35,11 +37,17 @@ DEBUG_FLAGS = -O -posix $(ARCHFLAGS) # OS release 3.2. Problems are supposed to be fixed # in release 3.3 (but at least for Sparc don't appear to be). # +# Defining HAS_BROKEN_IOCTL fixes problems with the old +# NeXT supplied serial drivers. +# +# Defining USE_MS_DNS will add support for the Microsoft +# DNS Negotation (for servers only). Thanks to +# clameter@hur.fuller.edu (Christoph Lameter) for the patches. # -COMPILE_FLAGS = -DNO_DRAND48 -DHAS_BROKEN_IOCTL \ +COMPILE_FLAGS = -DNO_DRAND48 \ -DDEBUGUPAP -DDEBUGCHAP -DDEBUGLCP -DDEBUGIPCP \ - -DFIXSIGS + -DFIXSIGS -DHAS_BROKEN_IOCTL -DUSE_MS_DNS COPTS = -O diff --git a/pppd/sys-NeXT.c b/pppd/sys-NeXT.c index 9def3fe..dcae3bf 100644 --- a/pppd/sys-NeXT.c +++ b/pppd/sys-NeXT.c @@ -20,7 +20,7 @@ */ #ifndef lint -static char rcsid[] = "$Id: sys-NeXT.c,v 1.2 1995/10/27 03:44:56 paulus Exp $"; +static char rcsid[] = "$Id: sys-NeXT.c,v 1.3 1995/12/18 03:30:47 paulus Exp $"; #endif #include @@ -332,6 +332,12 @@ struct speed { #ifdef B57600 { 57600, B57600 }, #endif +/* +#ifndef B115200 +#warning Defining B115200 +#define B115200 20 +#endif +*/ #ifdef B115200 { 115200, B115200 }, #endif @@ -399,6 +405,7 @@ set_up_tty(fd, local) tios.c_cflag |= CS8 | CREAD | HUPCL; if (local || !modem) tios.c_cflag |= CLOCAL; + tios.c_iflag = IGNBRK | IGNPAR; tios.c_oflag = 0; tios.c_lflag = 0; -- 2.39.2