Serial PPP Links andModems
-
serial link - sequential transport of bits/bytes; serial interface
on routers, PCs, modems and cell phones.
-
asynchronous (or synchronous - very expensive)
-
start, stop bits, inter-byte fill sequences etc, sent along with data
-
lower speeds <115.200, cheaper
-
parts of the link may go different speeds, compression, flow control
-
serial interfaces - RS232 (defines hardware flow control)
Internal modems - interrupt conflicts
If your computer already has a COM1 and COM2 serial port, then you will
need to install your internal modem as COM3 or COM4. Unfortunately, the
way the PC was originally designed, the choice of Interrupt Request (IRQ)
lines leads to a conflict, and won't work under Linux:
Port Linux device Base I/O Addr IRQ
------------------------------------------
COM1 ttyS0 3F8 4
COM2 ttyS1 2F8 3
COM3 ttyS2 3E8 4 [*] clashes with COM1
COM4 ttyS3 2E8 3 [*] clashes with COM2
The solution is to set your modem port to a different interrupt; good choices
are COM3-IRQ5 (unless you have a sound card, which is often already using
IRQ5) and COM4-IRQ2. (IRQ2 is the same as IRQ9 on a PC, so if your modem
has a link for IRQ9, that's fine too). Don't use IRQ7 - this is used by
the printer port.
Check the interrupts used on your machine with the command 'cat
/proc/interrupts' .
Once you have set up your modem, using on-board jumpers or DIP switches,
you need to tell Linux when it boots to use a different IRQ. Add a line
or lines like this to /etc/rc.d/rc.local
setserial -v /dev/ttyS2 irq 5
setserial -v /dev/ttyS3 irq 2
If you need to install an internal modem as COM1 or COM2, and your PC already
has a COM1 or COM2, then you must disable the existing COM port (possible
from BIOS if your PC motherboard is having this option). This might be
a jumper on your I/O card, or for motherboards with on-board I/O, a BIOS
setup setting.
External modems - old UARTs
The chip in your PC which converts parallel information into a serial data
stream to your modem, and back again, is called a UART (Universal Asynchronous
Receiver Transmitter). The original UART (part number 8250 or 16450) could
only hold one character at a time, which meant that if the processor didn't
fetch characters from it quickly enough, they could be lost. The new UART
is called a 16550A, and can buffer up to 16 characters, making data loss
much less likely and reducing the workload on the processor too.
It's easy to check which you have - look at the kernel messages when
the system boots up. If you have 8250 or 16450 UARTs, they are unlikely
to work at above 9600 bits per second (and even at 9600 may loose characters
occasionally). Upgrade your PCs I/O card to one which has 16550A UARTs;
this shouldn't cost you more than around US$30.
Most internal modems have 16550A UARTs on board - so this problem doesn't
arise for them. Most modern motherboards also have 16550A UARTs on-board.
Cables & connectors
A "straight" RS232 cable is used to connect a modem to a computer.
The modem end usually has a 25 pin female RS232 socket, and a computer
may have either a 9pin or a 25pin male socket. A "crossed" or "null modem"
RS232 cable is used to connect a terminal to a computer, and should not
be used to connect a modem.
A four wire RJ11 cable usually connects the phone line to the modem.
Ordinary phone lines use just two of the wires, but leased lines may use
all four.
Modems and Modem Configuration
Modems are devices that you use to connect your (digital) computer to an
(analog) phone line. The modem communicates with the computer over a serial
(COM) port. The modem is called the DCE (Data Communication Equipment),
and the computer is called the DTE (Data Terminal Equipment).
Initially, very low transfer rates of only 30 or 120 characters per
second (300 or 1200 bits per second (baud)) were the maximum speeds but
now even 28.8Kbps is now old fashioned the 56Kbps standard has emerged.
If files are not pre-compressed before transmission, in-transfer modem
compression (MNP5 or V42 protocols) can yield even higher effective transmission
speeds.
But attaining these connection speeds depends very much on the quality
of the telephone line and in some areas of most developing countries it
is still necessary to work at lower speeds. In some cases where the signal
strength is very low or if there is a lot of interference on the line,
it may be necessary to re-dial many times or to drop the modem speed to
2400 or even 1200 baud to ensure a reliable connection.
Modems vary considerably in quality. Ability to handle poor phone lines
varies considerably from one modem brand to another, and not necessarily
in ways that would be expected - some high-end popular modems are poor
at handling bad lines - for example US Robotics Sportsters. Modems that
work well under difficult conditions include the Zyxel and the Microcoms,
others may be just as good tho. If there are problems connecting it is
well worth trying out different modem brands.
Modems have four important settings:
-
the modulation speed, i.e. the speed at which they can transmit
data over the phone line. This could be anywhere between 300 bps and 56K
bps. This is sometimes known as the DCE speed.
-
the DTE speed, or the speed at which the modem communicates with
the computer. Since modems these days compress the data they send over
phone lines, they can actually talk to the computer at a higher rate than
they talk over the phone line. The DTE speed is usually fixed by the computer
to something like 38400 or 57600, if it has a high-speed (buffered 16550UART)
serial port. If not, it may be fixed at a lower speed.
-
Flow control. You should always use hardware flow control, also
known as RTS/CTS.
-
Error correction/compression - can be used if modems at both
ends support this. Most modems these days do. MNP and V.42/V.42bis are
some of the standards for error correction and data compression used by
modems.
For configuration, modems will use either AT commands, or DIP switches,
or have some configuration utility to let you set these options (e.g. via
an LCD panel in the front). Most modems are "Hayes-compatible" which refers
to the standard set of AT commands they can understand. Unfortunately the
Hayes "standard" isn't. Simple commands, like placing a call, are pretty
much the same between modems; but when you need to do something more complex,
such as setting how fast your modem communicates with a remote modem, you
have little choice but to dig out the manual and look up the command, since
the command set varies widely from one manufacturer to another.
There are also many different parameters you can set, and manufacturers
do not agree what is a "good" default setting to send from the factory.
For example, U.S. Robotics Sportsters used to ship with software handshaking
(XON/XOFF) enabled by default. However this setting interferes with many
dial-up protocols such as SLIP and XMODEM, and in general needs to be changed.
Different brands of modems have slightly different commands, so you need
to refer to your modem manual to find out exactly how this works for your
modem.
Dialup and leased line
Modems can be used in dialup mode on ordinary phone lines, or in
leased line mode. Not all modems can operate in leased line mode. Leased
line modems can usually operate in either synchronous or async mode, whereas
dialup modems usually operate in async mode only.
In dialup mode, the modem dials a telephone number, attempts to connect
to a modem which answers, and establishes a connection. In the leased line
mode, it expects to be permanently connected to a modem on the remote side.
Exercises
Setting up the Point-to-Point Protocol (PPP)
Setting up a PPP links with Linux is done with the ppp daemon, pppd,
which provides an IP transport layer for asynchronous serial interfaces.
The pppd daemon carries out the following
activities after it is run:
-
establishes the physical link
-
negotiating the ppp Link Control Protocol (LCP)
-
negotiating IP parameters
-
creating a ppp device (ppp0, ppp1...)
-
configuring the ppp device
-
adding a route to the ppp device.
There are many options to pppd, which are described in 'man pppd', but
the examples below cover the normal cases.
You can run ppp over an RS232 link directly connecting two PCs, like
this: (in one long line)
/usr/sbin/pppd ttyS0 57600 crtscts local passive persist
[your IP address]:[remote IP address] netmask 255.255.255.252
You can run kppp that is possible to configure and use from X-Windows
environment. The interface is very similar with Dial-up Networking of Microsoft
Windows 95/98.
Configure and Test your Modem
The most common method of configuring modems is using the Hayes AT commands.
For this, you need a program that lets you talk to the modem over the serial
port. On Unix machines, this is the cu command:
cu -l /dev/ttyS0 -s 57600 (if you get permission denied, do: chown uucp /dev/ttyS0)
opens a connection to /dev/ttyS0 at 57600 bps, and sends everything you
type to the modem, until terminated by a <return>~. sequence. So you
can type the following sequence to manually dial and login to a remote
machine. The basic setup for dialout is: AT &F1 &W - this reloads
the factory defaults and saves them to non-volatile RAM on your modem.
(Responses that you receive are shown in bold).
-
AT
-
OK
-
AT &F1 &W
-
OK
-
at&f1
-
OK
-
ati4 [displays the current settings for a USRobotics Courier V.]
B0 C1 E1 F1 M1 Q0
V1 X7
BAUD=57600 PARITY=N WORDLEN=8
DIAL=PULSE ON HOOK TIMER
&A3 &B1 &C1 &D2
&G0 &H1 &I0 &K1 &L0 &M4
&N0
&P0 &R2 &S0 &T5
&X0 &Y1 %N6
S00=000 S01=000 S02=043 S03=013
S04=010 S05=008 S06=002 S07=060
S08=002 S09=006 S10=007 S11=070
S12=050 S13=000 S14=001 S15=000
S16=000 S17=000 S18=000 S19=000
S20=000 S21=010 S22=017 S23=019
S24=150 S25=005 S26=001 S27=000
S28=008 S29=020 S30=000 S31=000
S32=009 S33=000 S34=000 S35=000
S36=000 S37=000 S38=000 S39=000
S40=000 S41=000 S42=126 S43=200
S44=015 S45=000 S46=000 S47=000
S48=000 S49=000 S50=000 S51=000
S52=000 S53=000 S54=064 S55=000
S56=000 S57=000
LAST DIALED #: T2020
OK
On the USR Courier, ATI4 shows the current settings in the modem (Most
other modems use AT&V for this).
The important settings shown above are:
&B1 -- Fix the DTE speed to current speed and lock it (57600 in our case)
&C1 -- Normal CD (i.e. CD goes on when connected only)
&D2 -- DTR normal (i.e. not forced high always)
&H1 -- Use hardware flow control
&K1 -- Negotiate modem compression and use if both modems agree
&M4 -- Negotiate modem error correction and use if both modems agree
S0=1 -- Auto-answer incoming calls
ATX0 -- Set for dumb dialling when the modem cannot detect your
dialtone, busy tone etc.
ATX4 -- Modem can detect dialtone, busy tone etc.
If any of these are not present, you will have to set them and save
the configuration using the AT&W command. e.g. To set &K1, you
would type AT&K1&W.
On USR modems some settings are set by DIP switches under the modem (e.g.
&D and &H settings are overridden by the DIP switches)
Also AT$H gives you some online help on the commands, and AT&$ and
AT%$ give you help on the & and % commands respectively (USRobotics
only)
Use ATDPphonenumber to dial out (ATDT is for tone dial lines). You
should see the modem go off-hook (the OH light comes on) and hear it dial.
Once it connects, the CD light comes on. You should then receive a login
prompt from the terminal server.
Login using the username of ppp with password ppp. You will
receive some garbage, which is actually the terminal server trying to start
up a PPP chat to you.
Use <return>~. to exit from cu. You should see both the TR and the CD
lights go off.
Setup dialout PPP
Objective: To configure your computer for dialout PPP.
-
Bring down the default route through the ethernet interface
route del default
-
Add debugging output to syslogd - edit /etc/syslog.conf and add:
*.=debug /var/log/debug and then restart syslogd - "killall -1 syslogd",
syslogd
-
Configure your modem for dialout
-
Dial out using cu and ATDTphone and manually login using the account
ppp and password ppp. Note the prompts that you get, since you will use
them to construct your chat script. Each of you uses the phone number based
on the diagram.
-
Dial out using pppd
pppd /dev/ttyS0 57600 \
connect 'chat -v "" AT OK ATDTPHONE ogin: ppp-user word: \\qpasswd'
\
modem crtscts debug defaultroute
The \\q ensures that your password is not echoed in the /var/log/messages.
Check that ppp is running. You can use tail -f /var/log/messages to
view what is happening.
-
To bring down the ppp connection by killing the pppd process. You can also
run 'ppp-off' if you have installed it from the /usr/doc/ppp* directory.
ppp will negotiate and bring down the link gracefully.
-
Tracing the ppp handshaking: When you use the debug option to pppd, it
logs the IPCP and LCP handshaking via syslog; by default RedHat linux prints
this information to /var/log/messages. You can also add an option of "kdebug
1" (or more) to see debugging messages from the kernel ppp driver. Similarly,
the "-v" argument to chat makes it log the entire dialogue to syslog.
-
You can have the whole chatscript on the commandline as shown above, but
if you use it frequently, it is more convenient to put it into a script,
e.g. chatscript, and use the command as:
pppd connect 'chat -f /etc/ppp/chatscript' /dev/ttyS0 modem crtscts
defaultroute and /etc/ppp/chatscript would contain:
TIMEOUT 3
ABORT '\nBUSY\r'
ABORT '\nNO ANSWER\r'
ABORT '\nRINGING\r\n\r\nRINGING\r'
'' \rAT
'OK-+++\c-OK' ATH0
TIMEOUT 60
OK ATDT1234
CONNECT ''
ogin:--ogin: ppp
assword: \qppp
-
There are also default scripts to start ppp, look in /usr/doc/ppp*/scripts.
You can copy the ppp-on, ppp-off and ppp-on-dialer to /etc/ppp and use
them (after entering the required information into /etc/ppp).
-
pppd has innumerable options -- look at the man page for full details.
Some of the more commonly used ones are:
-
defaultroute -- adds an entry to the routing table making this ppp link
your default route. However, it does not replace a default route if you
already have one.
-
debug -- sends debug info to syslog (with facility daemon, level debug)
-
[local]:[remote] -- assigns addresses to the link
-
netmask -- assigns netmask to the link
-
pppd allows you to specify options in a variety of ways -- you can have
a file called /etc/ppp/options with global options, overridden by /etc/ppp/options.ttyS0
with options for ppp on /dev/ttyS0 only, overridden by options in ~/.ppprc,
which can all be overridden by commandline options.
Authentication:
PAP authentication is done through the /etc/ppp/chap-secrets
file
the format is:
login domain password
normally 'domain' is not used unless required by the server, so a typical
example is:
js * alePest
Note passwords are not encrypted. The file is the same on the server
side, but it is also it is possible to tell pppd to use the /etc/passwd
file
How to set up your machine as a dialin
PPP server
-
Setup your computer to answer incoming calls
-
Install mgetty if /sbin/mgetty is not available on your machine.
-
Edit /etc/inittab to add a line for your modem:
d1:2345:respawn:/sbin/mgetty ttyS0
-
Reset init, using init q
-
Check, using ps -ax|grep mgetty that mgetty is running on the port
-
Create a user for ppp
-
Add a line into /etc/passwd as follows (use vipw):
ppp::501:501:PPP user:/:/usr/sbin/pppd
-
Then use passwd ppp to set the password of the ppp user to ppp.
-
Setup ppp to accept incoming calls
-
Edit the /etc/options/ppp.ttyS1 to have the address that you will give
your client. :169.222.N.N modem crtscts lock debug
-
Wait for a call
-
Check /var/log/messages and ifconfig to see what happens when you receive
a call.
-
We have setup routing for the network that you are willing to serve, so
that all traffic for your client is routed via you. Once the connection
is established, ask your neighbour to try pinging you and using traceroute
to an outside machine to check if this works.
Additional Notes:
-
There are two ways of making your modem answer incoming calls -- one is
to set the modem with ATS0 =1 (to answer after one ring), and the other
is to leave S0 at 0, but have an intelligent getty program that notices
the phone ring and sends an ATA command to the modem to answer. We prefer
the second.
-
Dynamic addressing: Note that the options.ttyXX is being used to dynamically
give an IP address to whoever dials into that port. You could also have
static addresses.
-
A much nicer terminal program than cu is minicom. Apart from having
a pretty screen display, you can use it to dial up to BBSes and transfer
files using X/Y/Zmodem. Beware, however, that in its normal configuration
minicom itself sends initialisation strings to the modem when you enter
it, and again when you leave. You can view and edit these with 'minicom
-s'
You should create or edit /etc/minirc.dfl (Red Hat) or /var/lib/minicom/minirc.dfl
(Slackware) so it looks like this:
pr port /dev/ttyS0
pu baudrate 38400
pu bits 8
pu parity N
Note that you should never use /dev/modem to refer to your modem - it creates
problems with lock files as one program might refer to the device as "/dev/modem"
while another refers to it as "/dev/ttyS0", for example.
To get minicom to give a nice colour display, add '-c on' to the command
line, or put the following line in /etc/profile:
export MINICOM='-c on'
There is another way of configuring a dialin ppp link - to use an intelligent
getty program that auto-senses PPP. (see below)
Regardless of the way the link is initiated, the options are usually
located in the ppp options file. The important things for a PPP server
to do are to allocate an IP address for the remote end of the link (if
it uses dynamic addresses, which is common for dialup PPP) and to handle
routing for the link when it comes up. Routing can be handled by adding
the proxyarp option to pppd, where the server responds to ARP queries on
the local LAN for the remote machine, and then routes packets down the
serial link. More common these days however, is to use a small network
for this link and route this network to the ppp link. The pppd program
automatically adds a host route for the machine at the other end of the
PPP link when the link comes up and deletes it when the link goes down.
For a dialout link, pppd needs to know the name of a connection program
(like chat), and (usually) a phone number, username and password that the
server recognises it by. These are usually part of the chat script. Also
needed are the serial port and speed to be used for the modem connection.
The PPP HOWTO
available with all Linux distributions covers PPP setup, both as client
and server, in detail.
-
mgetty and auto-sensing ppp:
-
For a Unix machine to accept logins on a serial port (either a directly
connected terminal or a modem) there is a program called getty that runs
on each line and waits for some action (either a telephone ringing, or
a user pressing enter on a directly connected terminal). mgetty is a "smart"
version of getty that can do a few more things than just detect a phone
ring and start the login program. mgetty is started by init (from inittab)
and waits for the phone to ring. It can then detect whether this is a data
or fax call, and answer appropriately. All this is controlled by a config
file (mgetty.config).
When PPP clients use the PAP (or CHAP) protocol for user authentication
and so do not need a separate login/password authentication at startup
then mgetty has a feature whereby it can sense the PPP start-up packets
and automatically start pppd (at the server end). The client end maintains
the username and password in the pap-secrets file (under unix pppd) and
authentication happens during the initial ppp chat. This simplifies the
chat script since you no longer need to worry about what exactly the login
prompt will look like.
By default mgetty disables this feature -- to enable it, you have to
recompile mgetty with -DAUTO_PPP (as documented) and uncomment the line
for AutoPPP in the login.config file. On the server side, pppd can either
use a pap-secrets file to authenticate the client, or, if given the "login"
option, uses the regular login (/etc/password) database.
Other programmes:
-
uugetty
-
An option to mgetty is uugetty, which can do many of the same things (allow
use of a modem on a serial port for both dial in and out). uugetty requires
a config file for each port that it answers on, usually
-
dip
-
dip is another program that handles dialup IP connections. It can do both
slip and ppp (but actually just calls pppd with the appropriate arguments
if you want ppp).
-
diald -- dial on demand
-
Another very useful feature is the ability to dial out automatically when
required and hang up the line when not in use. The diald program provides
this facility.
Link Quality Monitoring (LQM)
To ensure that the link can successfully transfer data, PPP monitors
its quality LQM quality report (LQR) packets. For link quality monitoring
to be operational, it only has to be enabled on one side of the link. If
the two machines are configured for different link quality reporting periods,
they negotiate to the lower value, so that the LQR period will be the same
on both sides of the link.
LQR packets contain counters of incoming and outgoing data packets for
the routers on each side of the link. Each time an LQR packet is received,
PPP uses that packet to calculate the outbound link quality (the percentage
of packets the router transmitted that its peer successfully receives)
and the inbound link quality (the percentage of packets that the peer transmitted
that this router successfully receives).
After five LQR reporting periods, PPP averages the inbound and outbound
link quality and compares these values against a user-specified threshold.
This is a rolling average. After the first five LQR reporting periods,
PPP acquires the data from the next period and drops the oldest data. Then
it computes the link quality average for that set of five LQR periods,
and so on.
If either the inbound or outbound link quality average drops below the
threshold, PPP brings down the link. The driver software automatically
brings the link back up and renegotiates the connection. PPP monitors the
link control packets flowing over the connection and resumes network control
protocol packet traffic when the link quality improves.
In addition to LQR packets, PPP periodically transmits echo-request packets
(when echo-request is enabled). If the peer transmits a user-specified
number of echo requests before receiving an echo reply from its peer router,
the router disables the link and restarts.