📄 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 990910== * * 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.33 1999/12/23 01:48:45 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>/* a macro to generate linux version number codes */#define VERSION(major,minor,patch) (((((major)<<8)+(minor))<<8)+(patch))#if LINUX_VERSION_CODE < VERSION(2,1,14)#include <linux/ioport.h>#endif#if LINUX_VERSION_CODE >= VERSION(2,1,23)#include <linux/poll.h>#endif#include <linux/in.h>#include <linux/malloc.h>#include <linux/tty.h>#include <linux/errno.h>#include <linux/sched.h> /* to get the struct task_struct */#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 <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#ifdef CONFIG_KERNELD#include <linux/kerneld.h>#endif#undef PPP_VERSION#define PPP_VERSION "2.3.11"#if LINUX_VERSION_CODE >= VERSION(2,1,4)#if LINUX_VERSION_CODE >= VERSION(2,1,5)#include <asm/uaccess.h>#else#include <asm/segment.h>#endif#define GET_USER get_user#define PUT_USER put_user#define COPY_FROM_USER copy_from_user#define COPY_TO_USER copy_to_user#else /* 2.0.x and 2.1.x before 2.1.4 */#define GET_USER(val, src) \ (verify_area(VERIFY_READ, src, sizeof(*src))? -EFAULT: \ ((val) = get_user(src), 0))#define PUT_USER(val, dst) \ (verify_area(VERIFY_WRITE, dst, sizeof(*dst))? -EFAULT: \ (put_user(val, dst), 0))#define COPY_FROM_USER(dst, src, size) \ (verify_area(VERIFY_READ, src, size)? -EFAULT: \ (memcpy_fromfs(dst, src, size), 0))#define COPY_TO_USER(dst, src, size) \ (verify_area(VERIFY_WRITE, dst, size)? -EFAULT: \ (memcpy_tofs(dst, src, size), 0))#endif /* < 2.1.4 */#if LINUX_VERSION_CODE < VERSION(2,1,37)#define test_and_set_bit(nr, addr) set_bit(nr, addr)#endif#if LINUX_VERSION_CODE < VERSION(2,1,25)#define net_device_stats enet_statistics#endif#if LINUX_VERSION_CODE < VERSION(2,1,57)#define signal_pending(tsk) ((tsk)->signal & ~(tsk)->blocked)#endif#if LINUX_VERSION_CODE < VERSION(2,1,60)typedef int rw_ret_t;typedef unsigned int rw_count_t;#elsetypedef ssize_t rw_ret_t;typedef size_t rw_count_t;#endif#if LINUX_VERSION_CODE < VERSION(2,1,86)#define KFREE_SKB(s) dev_kfree_skb((s), FREE_WRITE)#else#define KFREE_SKB(s) kfree_skb(s)#endif#if LINUX_VERSION_CODE < VERSION(2,1,15)#define LIBERATE_SKB(s) ((s)->free = 1)#else#define LIBERATE_SKB(s) do { } while (0)#endif#if LINUX_VERSION_CODE < VERSION(2,1,95)#define SUSER() suser()#else#define SUSER() capable(CAP_NET_ADMIN)#endif#if LINUX_VERSION_CODE < VERSION(2,2,0)#define wmb() mb()#endif/* * Local functions */static int ppp_register_compressor (struct compressor *cp);static void ppp_unregister_compressor (struct compressor *cp);static void ppp_async_init(struct ppp *ppp);static void ppp_async_release(struct ppp *ppp);static int ppp_tty_sync_push(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_sync_send(struct ppp *, struct sk_buff *);static void ppp_tty_flush_output(struct ppp *);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;#if LINUX_VERSION_CODE >= VERSION(2,1,19) MODULE_PARM(flag_time, "i");#endif#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;#if LINUX_VERSION_CODE < VERSION(2,1,18)static struct symbol_table ppp_syms = {#include <linux/symtab_begin.h> X(ppp_register_compressor), X(ppp_unregister_compressor),#include <linux/symtab_end.h>};#elseEXPORT_SYMBOL(ppp_register_compressor);EXPORT_SYMBOL(ppp_unregister_compressor);#endif/************************************************************* * 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 rw_ret_t ppp_tty_read(struct tty_struct *, struct file *, __u8 *, rw_count_t);static rw_ret_t ppp_tty_write(struct tty_struct *, struct file *, const __u8 *, rw_count_t);static int ppp_tty_ioctl(struct tty_struct *, struct file *, unsigned int, unsigned long);#if LINUX_VERSION_CODE < VERSION(2,1,23)static int ppp_tty_select(struct tty_struct *tty, struct inode *inode, struct file *filp, int sel_type, select_table * wait);#elsestatic unsigned int ppp_tty_poll(struct tty_struct *tty, struct file *filp, poll_table * wait);#endifstatic 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};#if LINUX_VERSION_CODE >= VERSION(2,1,18)EXPORT_SYMBOL(ppp_crc16_table);#endif#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;#if LINUX_VERSION_CODE >= VERSION(2,1,28) ppp_ldisc.name = "ppp";#endif 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;#if LINUX_VERSION_CODE < VERSION(2,1,23) ppp_ldisc.select = ppp_tty_select;#else ppp_ldisc.poll = ppp_tty_poll;#endif 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 LINUX_VERSION_CODE < VERSION(2,1,18) if (answer == 0) (void) register_symtab(&ppp_syms);#endif } 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) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -