📄 ppp.c
字号:
/* PPP for Linux * * Michael Callahan <callahan@maths.ox.ac.uk> * Al Longyear <longyear@netcom.com> * Extensively rewritten by Paul Mackerras <paulus@cs.anu.edu.au> * * ==FILEVERSION 990114== * * NOTE TO MAINTAINERS: * If you modify this file at all, please set the number above to the * date of the modification as YYMMDD (year month day). * ppp.c is shipped with a PPP distribution as well as with the kernel; * if everyone increases the FILEVERSION number above, then scripts * can do the right thing when deciding whether to install a new ppp.c * file. Don't change the format of that line otherwise, so the * installation script can recognize it. *//* Sources: slip.c RFC1331: The Point-to-Point Protocol (PPP) for the Transmission of Multi-protocol Datagrams over Point-to-Point Links RFC1332: IPCP ppp-2.0 Flags for this module (any combination is acceptable for testing.): OPTIMIZE_FLAG_TIME - Number of jiffies to force sending of leading flag character. This is normally set to ((HZ * 3) / 2). This is 1.5 seconds. If zero then the leading flag is always sent. CHECK_CHARACTERS - Enable the checking on all received characters for 8 data bits, no parity. This adds a small amount of processing for each received character.*/#define OPTIMIZE_FLAG_TIME ((HZ * 3)/2)#define CHECK_CHARACTERS 1#define PPP_MAX_RCV_QLEN 32 /* max # frames we queue up for pppd *//* $Id: ppp.c,v 1.19 1998/07/07 04:27:37 paulus Exp $ */#include <linux/version.h>#include <linux/config.h>#include <linux/module.h>#include <linux/kernel.h>#include <linux/sched.h>#include <linux/types.h>#include <linux/fcntl.h>#include <linux/interrupt.h>#include <linux/ptrace.h>#include <linux/poll.h>#include <linux/in.h>#include <linux/malloc.h>#include <linux/tty.h>#include <linux/errno.h>#include <linux/string.h> /* used in new tty drivers */#include <linux/signal.h> /* used in new tty drivers */#include <asm/system.h>#include <asm/bitops.h>#include <asm/uaccess.h>#include <linux/if.h>#include <linux/if_ether.h>#include <linux/netdevice.h>#include <linux/skbuff.h>#include <linux/inet.h>#include <linux/ioctl.h>#include <linux/ip.h>#include <linux/tcp.h>#include <linux/if_arp.h>#include <net/slhc_vj.h>#define fcstab ppp_crc16_table /* Name of the table in the kernel */#include <linux/ppp_defs.h>#include <linux/socket.h>#include <linux/if_ppp.h>#include <linux/if_pppvar.h>#include <linux/ppp-comp.h>#ifdef CONFIG_KMOD#include <linux/kmod.h>#endif/* * Local functions */#ifdef CONFIG_MODULESstatic int ppp_register_compressor (struct compressor *cp);static void ppp_unregister_compressor (struct compressor *cp);#endifstatic void ppp_async_init(struct ppp *ppp);static void ppp_async_release(struct ppp *ppp);static int ppp_tty_push(struct ppp *ppp);static int ppp_async_encode(struct ppp *ppp);static int ppp_async_send(struct ppp *, struct sk_buff *);static int ppp_ioctl(struct ppp *, unsigned int, unsigned long);static int ppp_set_compression (struct ppp *ppp, struct ppp_option_data *odp);static void ppp_proto_ccp(struct ppp *ppp, __u8 *dp, int len, int rcvd);static void ppp_ccp_closed(struct ppp *ppp);static int ppp_receive_frame(struct ppp *, struct sk_buff *);static void ppp_receive_error(struct ppp *ppp);static void ppp_output_wakeup(struct ppp *ppp);static void ppp_send_ctrl(struct ppp *ppp, struct sk_buff *skb);static void ppp_send_frame(struct ppp *ppp, struct sk_buff *skb);static void ppp_send_frames(struct ppp *ppp);static struct sk_buff *ppp_vj_compress(struct ppp *ppp, struct sk_buff *skb);static struct ppp *ppp_find (int pid_value);static struct ppp *ppp_alloc (void);static void ppp_generic_init(struct ppp *ppp);static void ppp_release(struct ppp *ppp);static void ppp_print_buffer (const char *, const __u8 *, int);static struct compressor *find_compressor (int type);#ifndef OPTIMIZE_FLAG_TIME#define OPTIMIZE_FLAG_TIME 0#endif/* * Parameters which may be changed via insmod. */static int flag_time = OPTIMIZE_FLAG_TIME;MODULE_PARM(flag_time, "i");#define CHECK_PPP_MAGIC(ppp) do { \ if (ppp->magic != PPP_MAGIC) { \ printk(ppp_magic_warn, ppp, __FILE__, __LINE__); \ } \} while (0)#define CHECK_PPP(a) do { \ CHECK_PPP_MAGIC(ppp); \ if (!ppp->inuse) { \ printk(ppp_warning, __LINE__); \ return a; \ } \} while (0)#define CHECK_PPP_VOID() do { \ CHECK_PPP_MAGIC(ppp); \ if (!ppp->inuse) { \ printk(ppp_warning, __LINE__); \ return; \ } \} while (0)#define tty2ppp(tty) ((struct ppp *) ((tty)->disc_data))#define dev2ppp(dev) ((struct ppp *) ((dev)->priv))#define ppp2tty(ppp) ((ppp)->tty)#define ppp2dev(ppp) (&(ppp)->dev)static struct ppp *ppp_list = NULL;static struct ppp *ppp_last = NULL;/* Define these strings only once for all macro invocations */static char ppp_warning[] = KERN_WARNING "PPP: ALERT! not INUSE! %d\n";static char ppp_magic_warn[] = KERN_WARNING "bad magic for ppp %p at %s:%d\n";static char szVersion[] = PPP_VERSION;EXPORT_SYMBOL(ppp_register_compressor);EXPORT_SYMBOL(ppp_unregister_compressor);/************************************************************* * LINE DISCIPLINE SUPPORT * The following code implements the PPP line discipline * and supports using PPP on an async serial line. *************************************************************/#define in_xmap(ppp,c) (ppp->xmit_async_map[(c) >> 5] & (1 << ((c) & 0x1f)))#define in_rmap(ppp,c) ((((unsigned int) (__u8) (c)) < 0x20) && \ ppp->recv_async_map & (1 << (c)))/* * TTY callbacks */static ssize_t ppp_tty_read(struct tty_struct *, struct file *, __u8 *, size_t);static ssize_t ppp_tty_write(struct tty_struct *, struct file *, const __u8 *, size_t);static int ppp_tty_ioctl(struct tty_struct *, struct file *, unsigned int, unsigned long);static unsigned int ppp_tty_poll(struct tty_struct *tty, struct file *filp, poll_table * wait);static int ppp_tty_open (struct tty_struct *);static void ppp_tty_close (struct tty_struct *);static int ppp_tty_room (struct tty_struct *tty);static void ppp_tty_receive (struct tty_struct *tty, const __u8 * cp, char *fp, int count);static void ppp_tty_wakeup (struct tty_struct *tty);__u16 ppp_crc16_table[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};EXPORT_SYMBOL(ppp_crc16_table);#ifdef CHECK_CHARACTERSstatic __u32 paritytab[8] ={ 0x96696996, 0x69969669, 0x69969669, 0x96696996, 0x69969669, 0x96696996, 0x96696996, 0x69969669};#endif/* * This procedure is called at initialization time to register * the PPP line discipline. */static intppp_first_time(void){ static struct tty_ldisc ppp_ldisc; int status; printk(KERN_INFO "PPP: version %s (demand dialling)" "\n", szVersion);#ifndef MODULE /* slhc module logic has its own copyright announcement */ printk(KERN_INFO "TCP compression code copyright 1989 Regents of the " "University of California\n");#endif /* * Register the tty discipline */ (void) memset (&ppp_ldisc, 0, sizeof (ppp_ldisc)); ppp_ldisc.magic = TTY_LDISC_MAGIC; ppp_ldisc.name = "ppp"; ppp_ldisc.open = ppp_tty_open; ppp_ldisc.close = ppp_tty_close; ppp_ldisc.read = ppp_tty_read; ppp_ldisc.write = ppp_tty_write; ppp_ldisc.ioctl = ppp_tty_ioctl; ppp_ldisc.poll = ppp_tty_poll; ppp_ldisc.receive_room = ppp_tty_room; ppp_ldisc.receive_buf = ppp_tty_receive; ppp_ldisc.write_wakeup = ppp_tty_wakeup; status = tty_register_ldisc (N_PPP, &ppp_ldisc); if (status == 0) printk(KERN_INFO "PPP line discipline registered.\n"); else printk(KERN_ERR "error registering line discipline: %d\n", status); return status;}#ifndef MODULE/* * Called at boot time if the PPP driver is compiled into the kernel. */intppp_init(struct device *dev){ static int first_time = 1; int answer = 0; if (first_time) { first_time = 0; answer = ppp_first_time(); } if (answer == 0) answer = -ENODEV; return answer;}#endif/* * Initialize the async-specific parts of the ppp structure. */static voidppp_async_init(struct ppp *ppp){ ppp->escape = 0; ppp->toss = 0xE0; ppp->tty_pushing = 0; memset (ppp->xmit_async_map, 0, sizeof (ppp->xmit_async_map)); ppp->xmit_async_map[0] = 0xffffffff; ppp->xmit_async_map[3] = 0x60000000; ppp->recv_async_map = 0xffffffff; ppp->tpkt = NULL; ppp->tfcs = PPP_INITFCS; ppp->optr = ppp->obuf; ppp->olim = ppp->obuf; ppp->rpkt = NULL; ppp->rfcs = PPP_INITFCS; ppp->tty = NULL; ppp->backup_tty = NULL; ppp->bytes_sent = 0; ppp->bytes_rcvd = 0;}/* * Clean up the async-specific parts of the ppp structure. */static voidppp_async_release(struct ppp *ppp){ struct sk_buff *skb; if ((skb = ppp->rpkt) != NULL) kfree_skb(skb); ppp->rpkt = NULL; if ((skb = ppp->tpkt) != NULL) kfree_skb(skb); ppp->tpkt = NULL;}/* * TTY callback. * * Called when the tty discipline is switched to PPP. */static intppp_tty_open (struct tty_struct *tty){ struct ppp *ppp; /* * Allocate a ppp structure to use. */ tty->disc_data = NULL; ppp = ppp_find(current->pid); if (ppp != NULL) { /* * If we are taking over a ppp unit which is currently * connected to a loopback pty, there's not much to do. */ CHECK_PPP(-EINVAL); } else { ppp = ppp_alloc(); if (ppp == NULL) { printk(KERN_ERR "ppp_alloc failed\n"); return -ENFILE; } /* * Initialize the control block */ ppp_generic_init(ppp); ppp_async_init(ppp); MOD_INC_USE_COUNT; } tty->disc_data = ppp; ppp->tty = tty; /* * Flush any pending characters in the driver */ if (tty->driver.flush_buffer) tty->driver.flush_buffer (tty); return ppp->line;}/* * TTY callback. * * Called when the line discipline is changed to something * else, the tty is closed, or the tty detects a hangup. */static voidppp_tty_close (struct tty_struct *tty){ struct ppp *ppp = tty2ppp(tty); if (ppp == NULL) return; tty->disc_data = NULL; if (ppp->magic != PPP_MAGIC) { printk(KERN_WARNING "ppp_tty_close: bogus\n"); return; } if (!ppp->inuse) { printk(KERN_WARNING "ppp_tty_close: not inuse\n"); ppp->tty = ppp->backup_tty = 0; return; } if (tty == ppp->backup_tty) ppp->backup_tty = 0; if (tty != ppp->tty) return; if (ppp->backup_tty) { ppp->tty = ppp->backup_tty; if (ppp_tty_push(ppp)) ppp_output_wakeup(ppp); } else { ppp->tty = 0; ppp->sc_xfer = 0; if (ppp->flags & SC_DEBUG) printk(KERN_INFO "ppp: channel %s closing.\n", ppp2dev(ppp)->name); ppp_async_release(ppp); ppp_release(ppp); MOD_DEC_USE_COUNT; }}/* * Read a PPP frame from the rcv_q list, * waiting if necessary */static ssize_tppp_tty_read(struct tty_struct *tty, struct file *file, __u8 * buf, size_t nr){ struct ppp *ppp = tty2ppp (tty); struct sk_buff *skb; ssize_t len, err; /* * Validate the pointers */ if (!ppp) return -EIO; CHECK_PPP(-ENXIO); /* * Before we attempt to write the frame to the user, ensure that the * user has access to the pages for the total buffer length. */ err = verify_area(VERIFY_WRITE, buf, nr); if (err != 0) return (err); /* * Wait for a frame to arrive if necessary. * We increment the module use count so that the module * can't go away while we're sleeping. */ MOD_INC_USE_COUNT; skb = NULL; for (;;) { ppp = tty2ppp(tty); err = 0; if (!ppp || ppp->magic != PPP_MAGIC || !ppp->inuse || tty != ppp->tty) break; skb = skb_dequeue(&ppp->rcv_q); if (skb != 0) break; /* * If no frame is available, return -EAGAIN or wait. */ err = -EAGAIN; if (file->f_flags & O_NONBLOCK) break; interruptible_sleep_on(&ppp->read_wait); err = -EINTR; if (signal_pending(current)) break; } MOD_DEC_USE_COUNT; if (skb == 0) return err; /* * Ensure that the frame will fit within the caller's buffer. * If not, just discard the frame. */ len = skb->len; if (len > nr) { if (ppp->flags & SC_DEBUG) printk(KERN_DEBUG "ppp: read of %lu bytes too small for %ld " "frame\n", (unsigned long) nr, (long) len); ppp->stats.ppp_ierrors++; err = -EOVERFLOW; goto out; } /* * Copy the received data from the buffer to the caller's area. */ err = len; if (copy_to_user(buf, skb->data, len)) err = -EFAULT;out: kfree_skb(skb); return err;}/* * Writing to a tty in ppp line discipline sends a PPP frame. * Used by pppd to send control packets (LCP, etc.). */static ssize_tppp_tty_write(struct tty_struct *tty, struct file *file, const __u8 * data, size_t count){ struct ppp *ppp = tty2ppp (tty); __u8 *new_data; struct sk_buff *skb; /* * Verify the pointers. */ if (!ppp) return -EIO; if (ppp->magic != PPP_MAGIC)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -