125763b3cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * Generic HDLC support routines for Linux 41da177e4SLinus Torvalds * Point-to-point protocol support 51da177e4SLinus Torvalds * 6e022c2f0SKrzysztof Hałasa * Copyright (C) 1999 - 2008 Krzysztof Halasa <khc@pm.waw.pl> 71da177e4SLinus Torvalds */ 81da177e4SLinus Torvalds 91da177e4SLinus Torvalds #include <linux/errno.h> 101da177e4SLinus Torvalds #include <linux/hdlc.h> 114dfce407SKrzysztof Hałasa #include <linux/if_arp.h> 124dfce407SKrzysztof Hałasa #include <linux/inetdevice.h> 134dfce407SKrzysztof Hałasa #include <linux/init.h> 144dfce407SKrzysztof Hałasa #include <linux/kernel.h> 154dfce407SKrzysztof Hałasa #include <linux/module.h> 164dfce407SKrzysztof Hałasa #include <linux/pkt_sched.h> 174dfce407SKrzysztof Hałasa #include <linux/poll.h> 184dfce407SKrzysztof Hałasa #include <linux/skbuff.h> 194dfce407SKrzysztof Hałasa #include <linux/slab.h> 20e022c2f0SKrzysztof Hałasa #include <linux/spinlock.h> 21eb2a2fd9SKrzysztof Halasa 22e022c2f0SKrzysztof Hałasa #define DEBUG_CP 0 /* also bytes# to dump */ 23e022c2f0SKrzysztof Hałasa #define DEBUG_STATE 0 24e022c2f0SKrzysztof Hałasa #define DEBUG_HARD_HEADER 0 25e022c2f0SKrzysztof Hałasa 26e022c2f0SKrzysztof Hałasa #define HDLC_ADDR_ALLSTATIONS 0xFF 27e022c2f0SKrzysztof Hałasa #define HDLC_CTRL_UI 0x03 28e022c2f0SKrzysztof Hałasa 29e022c2f0SKrzysztof Hałasa #define PID_LCP 0xC021 30e022c2f0SKrzysztof Hałasa #define PID_IP 0x0021 31e022c2f0SKrzysztof Hałasa #define PID_IPCP 0x8021 32e022c2f0SKrzysztof Hałasa #define PID_IPV6 0x0057 33e022c2f0SKrzysztof Hałasa #define PID_IPV6CP 0x8057 34e022c2f0SKrzysztof Hałasa 35e022c2f0SKrzysztof Hałasa enum {IDX_LCP = 0, IDX_IPCP, IDX_IPV6CP, IDX_COUNT}; 36e022c2f0SKrzysztof Hałasa enum {CP_CONF_REQ = 1, CP_CONF_ACK, CP_CONF_NAK, CP_CONF_REJ, CP_TERM_REQ, 37e022c2f0SKrzysztof Hałasa CP_TERM_ACK, CP_CODE_REJ, LCP_PROTO_REJ, LCP_ECHO_REQ, LCP_ECHO_REPLY, 38e022c2f0SKrzysztof Hałasa LCP_DISC_REQ, CP_CODES}; 39e022c2f0SKrzysztof Hałasa #if DEBUG_CP 40e022c2f0SKrzysztof Hałasa static const char *const code_names[CP_CODES] = { 41e022c2f0SKrzysztof Hałasa "0", "ConfReq", "ConfAck", "ConfNak", "ConfRej", "TermReq", 42e022c2f0SKrzysztof Hałasa "TermAck", "CodeRej", "ProtoRej", "EchoReq", "EchoReply", "Discard" 43eb2a2fd9SKrzysztof Halasa }; 44*2b57681fSPeng Li 45e022c2f0SKrzysztof Hałasa static char debug_buffer[64 + 3 * DEBUG_CP]; 46e022c2f0SKrzysztof Hałasa #endif 47e022c2f0SKrzysztof Hałasa 48e022c2f0SKrzysztof Hałasa enum {LCP_OPTION_MRU = 1, LCP_OPTION_ACCM, LCP_OPTION_MAGIC = 5}; 49e022c2f0SKrzysztof Hałasa 50e022c2f0SKrzysztof Hałasa struct hdlc_header { 51e022c2f0SKrzysztof Hałasa u8 address; 52e022c2f0SKrzysztof Hałasa u8 control; 53e022c2f0SKrzysztof Hałasa __be16 protocol; 54e022c2f0SKrzysztof Hałasa }; 55e022c2f0SKrzysztof Hałasa 56e022c2f0SKrzysztof Hałasa struct cp_header { 57e022c2f0SKrzysztof Hałasa u8 code; 58e022c2f0SKrzysztof Hałasa u8 id; 59e022c2f0SKrzysztof Hałasa __be16 len; 60e022c2f0SKrzysztof Hałasa }; 61e022c2f0SKrzysztof Hałasa 62e022c2f0SKrzysztof Hałasa struct proto { 63e022c2f0SKrzysztof Hałasa struct net_device *dev; 64e022c2f0SKrzysztof Hałasa struct timer_list timer; 65e022c2f0SKrzysztof Hałasa unsigned long timeout; 66e022c2f0SKrzysztof Hałasa u16 pid; /* protocol ID */ 67e022c2f0SKrzysztof Hałasa u8 state; 68e022c2f0SKrzysztof Hałasa u8 cr_id; /* ID of last Configuration-Request */ 69e022c2f0SKrzysztof Hałasa u8 restart_counter; 70e022c2f0SKrzysztof Hałasa }; 71e022c2f0SKrzysztof Hałasa 72e022c2f0SKrzysztof Hałasa struct ppp { 73e022c2f0SKrzysztof Hałasa struct proto protos[IDX_COUNT]; 74e022c2f0SKrzysztof Hałasa spinlock_t lock; 75e022c2f0SKrzysztof Hałasa unsigned long last_pong; 76e022c2f0SKrzysztof Hałasa unsigned int req_timeout, cr_retries, term_retries; 77e022c2f0SKrzysztof Hałasa unsigned int keepalive_interval, keepalive_timeout; 78e022c2f0SKrzysztof Hałasa u8 seq; /* local sequence number for requests */ 79e022c2f0SKrzysztof Hałasa u8 echo_id; /* ID of last Echo-Request (LCP) */ 80e022c2f0SKrzysztof Hałasa }; 81e022c2f0SKrzysztof Hałasa 82e022c2f0SKrzysztof Hałasa enum {CLOSED = 0, STOPPED, STOPPING, REQ_SENT, ACK_RECV, ACK_SENT, OPENED, 83e022c2f0SKrzysztof Hałasa STATES, STATE_MASK = 0xF}; 84e022c2f0SKrzysztof Hałasa enum {START = 0, STOP, TO_GOOD, TO_BAD, RCR_GOOD, RCR_BAD, RCA, RCN, RTR, RTA, 85e022c2f0SKrzysztof Hałasa RUC, RXJ_GOOD, RXJ_BAD, EVENTS}; 86e022c2f0SKrzysztof Hałasa enum {INV = 0x10, IRC = 0x20, ZRC = 0x40, SCR = 0x80, SCA = 0x100, 87e022c2f0SKrzysztof Hałasa SCN = 0x200, STR = 0x400, STA = 0x800, SCJ = 0x1000}; 88e022c2f0SKrzysztof Hałasa 89e022c2f0SKrzysztof Hałasa #if DEBUG_STATE 90e022c2f0SKrzysztof Hałasa static const char *const state_names[STATES] = { 91e022c2f0SKrzysztof Hałasa "Closed", "Stopped", "Stopping", "ReqSent", "AckRecv", "AckSent", 92e022c2f0SKrzysztof Hałasa "Opened" 93e022c2f0SKrzysztof Hałasa }; 94*2b57681fSPeng Li 95e022c2f0SKrzysztof Hałasa static const char *const event_names[EVENTS] = { 96e022c2f0SKrzysztof Hałasa "Start", "Stop", "TO+", "TO-", "RCR+", "RCR-", "RCA", "RCN", 97e022c2f0SKrzysztof Hałasa "RTR", "RTA", "RUC", "RXJ+", "RXJ-" 98e022c2f0SKrzysztof Hałasa }; 99e022c2f0SKrzysztof Hałasa #endif 100e022c2f0SKrzysztof Hałasa 101e022c2f0SKrzysztof Hałasa static struct sk_buff_head tx_queue; /* used when holding the spin lock */ 102eb2a2fd9SKrzysztof Halasa 103eb2a2fd9SKrzysztof Halasa static int ppp_ioctl(struct net_device *dev, struct ifreq *ifr); 104eb2a2fd9SKrzysztof Halasa 105e022c2f0SKrzysztof Hałasa static inline struct ppp* get_ppp(struct net_device *dev) 106eb2a2fd9SKrzysztof Halasa { 107e022c2f0SKrzysztof Hałasa return (struct ppp *)dev_to_hdlc(dev)->state; 108eb2a2fd9SKrzysztof Halasa } 1091da177e4SLinus Torvalds 110e022c2f0SKrzysztof Hałasa static inline struct proto* get_proto(struct net_device *dev, u16 pid) 1111da177e4SLinus Torvalds { 112e022c2f0SKrzysztof Hałasa struct ppp *ppp = get_ppp(dev); 1131da177e4SLinus Torvalds 114e022c2f0SKrzysztof Hałasa switch (pid) { 115e022c2f0SKrzysztof Hałasa case PID_LCP: 116e022c2f0SKrzysztof Hałasa return &ppp->protos[IDX_LCP]; 117e022c2f0SKrzysztof Hałasa case PID_IPCP: 118e022c2f0SKrzysztof Hałasa return &ppp->protos[IDX_IPCP]; 119e022c2f0SKrzysztof Hałasa case PID_IPV6CP: 120e022c2f0SKrzysztof Hałasa return &ppp->protos[IDX_IPV6CP]; 121e022c2f0SKrzysztof Hałasa default: 122e022c2f0SKrzysztof Hałasa return NULL; 123e022c2f0SKrzysztof Hałasa } 1241da177e4SLinus Torvalds } 1251da177e4SLinus Torvalds 126e022c2f0SKrzysztof Hałasa static inline const char* proto_name(u16 pid) 1271da177e4SLinus Torvalds { 128e022c2f0SKrzysztof Hałasa switch (pid) { 129e022c2f0SKrzysztof Hałasa case PID_LCP: 130e022c2f0SKrzysztof Hałasa return "LCP"; 131e022c2f0SKrzysztof Hałasa case PID_IPCP: 132e022c2f0SKrzysztof Hałasa return "IPCP"; 133e022c2f0SKrzysztof Hałasa case PID_IPV6CP: 134e022c2f0SKrzysztof Hałasa return "IPV6CP"; 135e022c2f0SKrzysztof Hałasa default: 136e022c2f0SKrzysztof Hałasa return NULL; 1371da177e4SLinus Torvalds } 138e022c2f0SKrzysztof Hałasa } 1391da177e4SLinus Torvalds 140ab611487SAlexey Dobriyan static __be16 ppp_type_trans(struct sk_buff *skb, struct net_device *dev) 1411da177e4SLinus Torvalds { 142e022c2f0SKrzysztof Hałasa struct hdlc_header *data = (struct hdlc_header*)skb->data; 143e022c2f0SKrzysztof Hałasa 144e022c2f0SKrzysztof Hałasa if (skb->len < sizeof(struct hdlc_header)) 145e022c2f0SKrzysztof Hałasa return htons(ETH_P_HDLC); 146e022c2f0SKrzysztof Hałasa if (data->address != HDLC_ADDR_ALLSTATIONS || 147e022c2f0SKrzysztof Hałasa data->control != HDLC_CTRL_UI) 148e022c2f0SKrzysztof Hałasa return htons(ETH_P_HDLC); 149e022c2f0SKrzysztof Hałasa 150e022c2f0SKrzysztof Hałasa switch (data->protocol) { 15109640e63SHarvey Harrison case cpu_to_be16(PID_IP): 152e022c2f0SKrzysztof Hałasa skb_pull(skb, sizeof(struct hdlc_header)); 153e022c2f0SKrzysztof Hałasa return htons(ETH_P_IP); 154e022c2f0SKrzysztof Hałasa 15509640e63SHarvey Harrison case cpu_to_be16(PID_IPV6): 156e022c2f0SKrzysztof Hałasa skb_pull(skb, sizeof(struct hdlc_header)); 157e022c2f0SKrzysztof Hałasa return htons(ETH_P_IPV6); 158e022c2f0SKrzysztof Hałasa 159e022c2f0SKrzysztof Hałasa default: 160e022c2f0SKrzysztof Hałasa return htons(ETH_P_HDLC); 161e022c2f0SKrzysztof Hałasa } 1621da177e4SLinus Torvalds } 1631da177e4SLinus Torvalds 164e022c2f0SKrzysztof Hałasa static int ppp_hard_header(struct sk_buff *skb, struct net_device *dev, 165e022c2f0SKrzysztof Hałasa u16 type, const void *daddr, const void *saddr, 166e022c2f0SKrzysztof Hałasa unsigned int len) 167e022c2f0SKrzysztof Hałasa { 168e022c2f0SKrzysztof Hałasa struct hdlc_header *data; 169e022c2f0SKrzysztof Hałasa #if DEBUG_HARD_HEADER 170e022c2f0SKrzysztof Hałasa printk(KERN_DEBUG "%s: ppp_hard_header() called\n", dev->name); 171e022c2f0SKrzysztof Hałasa #endif 172e022c2f0SKrzysztof Hałasa 173e022c2f0SKrzysztof Hałasa skb_push(skb, sizeof(struct hdlc_header)); 174e022c2f0SKrzysztof Hałasa data = (struct hdlc_header*)skb->data; 175e022c2f0SKrzysztof Hałasa 176e022c2f0SKrzysztof Hałasa data->address = HDLC_ADDR_ALLSTATIONS; 177e022c2f0SKrzysztof Hałasa data->control = HDLC_CTRL_UI; 178e022c2f0SKrzysztof Hałasa switch (type) { 179e022c2f0SKrzysztof Hałasa case ETH_P_IP: 180e022c2f0SKrzysztof Hałasa data->protocol = htons(PID_IP); 181e022c2f0SKrzysztof Hałasa break; 182e022c2f0SKrzysztof Hałasa case ETH_P_IPV6: 183e022c2f0SKrzysztof Hałasa data->protocol = htons(PID_IPV6); 184e022c2f0SKrzysztof Hałasa break; 185e022c2f0SKrzysztof Hałasa case PID_LCP: 186e022c2f0SKrzysztof Hałasa case PID_IPCP: 187e022c2f0SKrzysztof Hałasa case PID_IPV6CP: 188e022c2f0SKrzysztof Hałasa data->protocol = htons(type); 189e022c2f0SKrzysztof Hałasa break; 190e022c2f0SKrzysztof Hałasa default: /* unknown protocol */ 191e022c2f0SKrzysztof Hałasa data->protocol = 0; 192e022c2f0SKrzysztof Hałasa } 193e022c2f0SKrzysztof Hałasa return sizeof(struct hdlc_header); 194e022c2f0SKrzysztof Hałasa } 195e022c2f0SKrzysztof Hałasa 196e022c2f0SKrzysztof Hałasa static void ppp_tx_flush(void) 197e022c2f0SKrzysztof Hałasa { 198e022c2f0SKrzysztof Hałasa struct sk_buff *skb; 199*2b57681fSPeng Li 200e022c2f0SKrzysztof Hałasa while ((skb = skb_dequeue(&tx_queue)) != NULL) 201e022c2f0SKrzysztof Hałasa dev_queue_xmit(skb); 202e022c2f0SKrzysztof Hałasa } 203e022c2f0SKrzysztof Hałasa 204e022c2f0SKrzysztof Hałasa static void ppp_tx_cp(struct net_device *dev, u16 pid, u8 code, 205e022c2f0SKrzysztof Hałasa u8 id, unsigned int len, const void *data) 206e022c2f0SKrzysztof Hałasa { 207e022c2f0SKrzysztof Hałasa struct sk_buff *skb; 208e022c2f0SKrzysztof Hałasa struct cp_header *cp; 209e022c2f0SKrzysztof Hałasa unsigned int magic_len = 0; 210e022c2f0SKrzysztof Hałasa static u32 magic; 211e022c2f0SKrzysztof Hałasa 212e022c2f0SKrzysztof Hałasa #if DEBUG_CP 213e022c2f0SKrzysztof Hałasa int i; 214e022c2f0SKrzysztof Hałasa char *ptr; 215e022c2f0SKrzysztof Hałasa #endif 216e022c2f0SKrzysztof Hałasa 217e022c2f0SKrzysztof Hałasa if (pid == PID_LCP && (code == LCP_ECHO_REQ || code == LCP_ECHO_REPLY)) 218e022c2f0SKrzysztof Hałasa magic_len = sizeof(magic); 219e022c2f0SKrzysztof Hałasa 220e022c2f0SKrzysztof Hałasa skb = dev_alloc_skb(sizeof(struct hdlc_header) + 221e022c2f0SKrzysztof Hałasa sizeof(struct cp_header) + magic_len + len); 222e022c2f0SKrzysztof Hałasa if (!skb) { 22312a3bfefSJoe Perches netdev_warn(dev, "out of memory in ppp_tx_cp()\n"); 224e022c2f0SKrzysztof Hałasa return; 225e022c2f0SKrzysztof Hałasa } 226e022c2f0SKrzysztof Hałasa skb_reserve(skb, sizeof(struct hdlc_header)); 227e022c2f0SKrzysztof Hałasa 2284df864c1SJohannes Berg cp = skb_put(skb, sizeof(struct cp_header)); 229e022c2f0SKrzysztof Hałasa cp->code = code; 230e022c2f0SKrzysztof Hałasa cp->id = id; 231e022c2f0SKrzysztof Hałasa cp->len = htons(sizeof(struct cp_header) + magic_len + len); 232e022c2f0SKrzysztof Hałasa 233e022c2f0SKrzysztof Hałasa if (magic_len) 23459ae1d12SJohannes Berg skb_put_data(skb, &magic, magic_len); 235e022c2f0SKrzysztof Hałasa if (len) 23659ae1d12SJohannes Berg skb_put_data(skb, data, len); 237e022c2f0SKrzysztof Hałasa 238e022c2f0SKrzysztof Hałasa #if DEBUG_CP 239e022c2f0SKrzysztof Hałasa BUG_ON(code >= CP_CODES); 240e022c2f0SKrzysztof Hałasa ptr = debug_buffer; 241e022c2f0SKrzysztof Hałasa *ptr = '\x0'; 242e022c2f0SKrzysztof Hałasa for (i = 0; i < min_t(unsigned int, magic_len + len, DEBUG_CP); i++) { 243e022c2f0SKrzysztof Hałasa sprintf(ptr, " %02X", skb->data[sizeof(struct cp_header) + i]); 244e022c2f0SKrzysztof Hałasa ptr += strlen(ptr); 245e022c2f0SKrzysztof Hałasa } 246e022c2f0SKrzysztof Hałasa printk(KERN_DEBUG "%s: TX %s [%s id 0x%X]%s\n", dev->name, 247e022c2f0SKrzysztof Hałasa proto_name(pid), code_names[code], id, debug_buffer); 248e022c2f0SKrzysztof Hałasa #endif 249e022c2f0SKrzysztof Hałasa 250e022c2f0SKrzysztof Hałasa ppp_hard_header(skb, dev, pid, NULL, NULL, 0); 251e022c2f0SKrzysztof Hałasa 252e022c2f0SKrzysztof Hałasa skb->priority = TC_PRIO_CONTROL; 253e022c2f0SKrzysztof Hałasa skb->dev = dev; 2549fb030a7SXie He skb->protocol = htons(ETH_P_HDLC); 255e022c2f0SKrzysztof Hałasa skb_reset_network_header(skb); 256e022c2f0SKrzysztof Hałasa skb_queue_tail(&tx_queue, skb); 257e022c2f0SKrzysztof Hałasa } 258e022c2f0SKrzysztof Hałasa 259e022c2f0SKrzysztof Hałasa /* State transition table (compare STD-51) 260e022c2f0SKrzysztof Hałasa Events Actions 261e022c2f0SKrzysztof Hałasa TO+ = Timeout with counter > 0 irc = Initialize-Restart-Count 262e022c2f0SKrzysztof Hałasa TO- = Timeout with counter expired zrc = Zero-Restart-Count 263e022c2f0SKrzysztof Hałasa 264e022c2f0SKrzysztof Hałasa RCR+ = Receive-Configure-Request (Good) scr = Send-Configure-Request 265e022c2f0SKrzysztof Hałasa RCR- = Receive-Configure-Request (Bad) 266e022c2f0SKrzysztof Hałasa RCA = Receive-Configure-Ack sca = Send-Configure-Ack 267e022c2f0SKrzysztof Hałasa RCN = Receive-Configure-Nak/Rej scn = Send-Configure-Nak/Rej 268e022c2f0SKrzysztof Hałasa 269e022c2f0SKrzysztof Hałasa RTR = Receive-Terminate-Request str = Send-Terminate-Request 270e022c2f0SKrzysztof Hałasa RTA = Receive-Terminate-Ack sta = Send-Terminate-Ack 271e022c2f0SKrzysztof Hałasa 272e022c2f0SKrzysztof Hałasa RUC = Receive-Unknown-Code scj = Send-Code-Reject 273e022c2f0SKrzysztof Hałasa RXJ+ = Receive-Code-Reject (permitted) 274e022c2f0SKrzysztof Hałasa or Receive-Protocol-Reject 275e022c2f0SKrzysztof Hałasa RXJ- = Receive-Code-Reject (catastrophic) 276e022c2f0SKrzysztof Hałasa or Receive-Protocol-Reject 277e022c2f0SKrzysztof Hałasa */ 278e022c2f0SKrzysztof Hałasa static int cp_table[EVENTS][STATES] = { 279e022c2f0SKrzysztof Hałasa /* CLOSED STOPPED STOPPING REQ_SENT ACK_RECV ACK_SENT OPENED 280e022c2f0SKrzysztof Hałasa 0 1 2 3 4 5 6 */ 281e022c2f0SKrzysztof Hałasa {IRC|SCR|3, INV , INV , INV , INV , INV , INV }, /* START */ 282e022c2f0SKrzysztof Hałasa { INV , 0 , 0 , 0 , 0 , 0 , 0 }, /* STOP */ 283e022c2f0SKrzysztof Hałasa { INV , INV ,STR|2, SCR|3 ,SCR|3, SCR|5 , INV }, /* TO+ */ 284e022c2f0SKrzysztof Hałasa { INV , INV , 1 , 1 , 1 , 1 , INV }, /* TO- */ 285e022c2f0SKrzysztof Hałasa { STA|0 ,IRC|SCR|SCA|5, 2 , SCA|5 ,SCA|6, SCA|5 ,SCR|SCA|5}, /* RCR+ */ 286e022c2f0SKrzysztof Hałasa { STA|0 ,IRC|SCR|SCN|3, 2 , SCN|3 ,SCN|4, SCN|3 ,SCR|SCN|3}, /* RCR- */ 287e022c2f0SKrzysztof Hałasa { STA|0 , STA|1 , 2 , IRC|4 ,SCR|3, 6 , SCR|3 }, /* RCA */ 288e022c2f0SKrzysztof Hałasa { STA|0 , STA|1 , 2 ,IRC|SCR|3,SCR|3,IRC|SCR|5, SCR|3 }, /* RCN */ 289e022c2f0SKrzysztof Hałasa { STA|0 , STA|1 ,STA|2, STA|3 ,STA|3, STA|3 ,ZRC|STA|2}, /* RTR */ 290e022c2f0SKrzysztof Hałasa { 0 , 1 , 1 , 3 , 3 , 5 , SCR|3 }, /* RTA */ 291e022c2f0SKrzysztof Hałasa { SCJ|0 , SCJ|1 ,SCJ|2, SCJ|3 ,SCJ|4, SCJ|5 , SCJ|6 }, /* RUC */ 292e022c2f0SKrzysztof Hałasa { 0 , 1 , 2 , 3 , 3 , 5 , 6 }, /* RXJ+ */ 293e022c2f0SKrzysztof Hałasa { 0 , 1 , 1 , 1 , 1 , 1 ,IRC|STR|2}, /* RXJ- */ 294e022c2f0SKrzysztof Hałasa }; 295e022c2f0SKrzysztof Hałasa 296e022c2f0SKrzysztof Hałasa /* SCA: RCR+ must supply id, len and data 297e022c2f0SKrzysztof Hałasa SCN: RCR- must supply code, id, len and data 298e022c2f0SKrzysztof Hałasa STA: RTR must supply id 299e022c2f0SKrzysztof Hałasa SCJ: RUC must supply CP packet len and data */ 300e022c2f0SKrzysztof Hałasa static void ppp_cp_event(struct net_device *dev, u16 pid, u16 event, u8 code, 30193bc9335SKrzysztof Hałasa u8 id, unsigned int len, const void *data) 302e022c2f0SKrzysztof Hałasa { 303e022c2f0SKrzysztof Hałasa int old_state, action; 304e022c2f0SKrzysztof Hałasa struct ppp *ppp = get_ppp(dev); 305e022c2f0SKrzysztof Hałasa struct proto *proto = get_proto(dev, pid); 306e022c2f0SKrzysztof Hałasa 307e022c2f0SKrzysztof Hałasa old_state = proto->state; 308e022c2f0SKrzysztof Hałasa BUG_ON(old_state >= STATES); 309e022c2f0SKrzysztof Hałasa BUG_ON(event >= EVENTS); 310e022c2f0SKrzysztof Hałasa 311e022c2f0SKrzysztof Hałasa #if DEBUG_STATE 312e022c2f0SKrzysztof Hałasa printk(KERN_DEBUG "%s: %s ppp_cp_event(%s) %s ...\n", dev->name, 313e022c2f0SKrzysztof Hałasa proto_name(pid), event_names[event], state_names[proto->state]); 314e022c2f0SKrzysztof Hałasa #endif 315e022c2f0SKrzysztof Hałasa 316e022c2f0SKrzysztof Hałasa action = cp_table[event][old_state]; 317e022c2f0SKrzysztof Hałasa 318e022c2f0SKrzysztof Hałasa proto->state = action & STATE_MASK; 319e022c2f0SKrzysztof Hałasa if (action & (SCR | STR)) /* set Configure-Req/Terminate-Req timer */ 320e022c2f0SKrzysztof Hałasa mod_timer(&proto->timer, proto->timeout = 321e022c2f0SKrzysztof Hałasa jiffies + ppp->req_timeout * HZ); 322e022c2f0SKrzysztof Hałasa if (action & ZRC) 323e022c2f0SKrzysztof Hałasa proto->restart_counter = 0; 324e022c2f0SKrzysztof Hałasa if (action & IRC) 325e022c2f0SKrzysztof Hałasa proto->restart_counter = (proto->state == STOPPING) ? 326e022c2f0SKrzysztof Hałasa ppp->term_retries : ppp->cr_retries; 327e022c2f0SKrzysztof Hałasa 328e022c2f0SKrzysztof Hałasa if (action & SCR) /* send Configure-Request */ 329e022c2f0SKrzysztof Hałasa ppp_tx_cp(dev, pid, CP_CONF_REQ, proto->cr_id = ++ppp->seq, 330e022c2f0SKrzysztof Hałasa 0, NULL); 331e022c2f0SKrzysztof Hałasa if (action & SCA) /* send Configure-Ack */ 332e022c2f0SKrzysztof Hałasa ppp_tx_cp(dev, pid, CP_CONF_ACK, id, len, data); 333e022c2f0SKrzysztof Hałasa if (action & SCN) /* send Configure-Nak/Reject */ 334e022c2f0SKrzysztof Hałasa ppp_tx_cp(dev, pid, code, id, len, data); 335e022c2f0SKrzysztof Hałasa if (action & STR) /* send Terminate-Request */ 336e022c2f0SKrzysztof Hałasa ppp_tx_cp(dev, pid, CP_TERM_REQ, ++ppp->seq, 0, NULL); 337e022c2f0SKrzysztof Hałasa if (action & STA) /* send Terminate-Ack */ 338e022c2f0SKrzysztof Hałasa ppp_tx_cp(dev, pid, CP_TERM_ACK, id, 0, NULL); 339e022c2f0SKrzysztof Hałasa if (action & SCJ) /* send Code-Reject */ 340e022c2f0SKrzysztof Hałasa ppp_tx_cp(dev, pid, CP_CODE_REJ, ++ppp->seq, len, data); 341e022c2f0SKrzysztof Hałasa 342e022c2f0SKrzysztof Hałasa if (old_state != OPENED && proto->state == OPENED) { 34312a3bfefSJoe Perches netdev_info(dev, "%s up\n", proto_name(pid)); 344e022c2f0SKrzysztof Hałasa if (pid == PID_LCP) { 345e022c2f0SKrzysztof Hałasa netif_dormant_off(dev); 346e022c2f0SKrzysztof Hałasa ppp_cp_event(dev, PID_IPCP, START, 0, 0, 0, NULL); 347e022c2f0SKrzysztof Hałasa ppp_cp_event(dev, PID_IPV6CP, START, 0, 0, 0, NULL); 348e022c2f0SKrzysztof Hałasa ppp->last_pong = jiffies; 349e022c2f0SKrzysztof Hałasa mod_timer(&proto->timer, proto->timeout = 350e022c2f0SKrzysztof Hałasa jiffies + ppp->keepalive_interval * HZ); 351e022c2f0SKrzysztof Hałasa } 352e022c2f0SKrzysztof Hałasa } 353e022c2f0SKrzysztof Hałasa if (old_state == OPENED && proto->state != OPENED) { 35412a3bfefSJoe Perches netdev_info(dev, "%s down\n", proto_name(pid)); 355e022c2f0SKrzysztof Hałasa if (pid == PID_LCP) { 356e022c2f0SKrzysztof Hałasa netif_dormant_on(dev); 357e022c2f0SKrzysztof Hałasa ppp_cp_event(dev, PID_IPCP, STOP, 0, 0, 0, NULL); 358e022c2f0SKrzysztof Hałasa ppp_cp_event(dev, PID_IPV6CP, STOP, 0, 0, 0, NULL); 359e022c2f0SKrzysztof Hałasa } 360e022c2f0SKrzysztof Hałasa } 361e022c2f0SKrzysztof Hałasa if (old_state != CLOSED && proto->state == CLOSED) 362e022c2f0SKrzysztof Hałasa del_timer(&proto->timer); 363e022c2f0SKrzysztof Hałasa 364e022c2f0SKrzysztof Hałasa #if DEBUG_STATE 365e022c2f0SKrzysztof Hałasa printk(KERN_DEBUG "%s: %s ppp_cp_event(%s) ... %s\n", dev->name, 366e022c2f0SKrzysztof Hałasa proto_name(pid), event_names[event], state_names[proto->state]); 367e022c2f0SKrzysztof Hałasa #endif 368e022c2f0SKrzysztof Hałasa } 369e022c2f0SKrzysztof Hałasa 370e022c2f0SKrzysztof Hałasa static void ppp_cp_parse_cr(struct net_device *dev, u16 pid, u8 id, 37193bc9335SKrzysztof Hałasa unsigned int req_len, const u8 *data) 372e022c2f0SKrzysztof Hałasa { 373e022c2f0SKrzysztof Hałasa static u8 const valid_accm[6] = { LCP_OPTION_ACCM, 6, 0, 0, 0, 0 }; 37493bc9335SKrzysztof Hałasa const u8 *opt; 37593bc9335SKrzysztof Hałasa u8 *out; 37693bc9335SKrzysztof Hałasa unsigned int len = req_len, nak_len = 0, rej_len = 0; 377e022c2f0SKrzysztof Hałasa 378e022c2f0SKrzysztof Hałasa if (!(out = kmalloc(len, GFP_ATOMIC))) { 379e022c2f0SKrzysztof Hałasa dev->stats.rx_dropped++; 380e022c2f0SKrzysztof Hałasa return; /* out of memory, ignore CR packet */ 381e022c2f0SKrzysztof Hałasa } 382e022c2f0SKrzysztof Hałasa 383e022c2f0SKrzysztof Hałasa for (opt = data; len; len -= opt[1], opt += opt[1]) { 38466d42ed8SDan Carpenter if (len < 2 || opt[1] < 2 || len < opt[1]) 38566d42ed8SDan Carpenter goto err_out; 386e022c2f0SKrzysztof Hałasa 387e022c2f0SKrzysztof Hałasa if (pid == PID_LCP) 388e022c2f0SKrzysztof Hałasa switch (opt[0]) { 389e022c2f0SKrzysztof Hałasa case LCP_OPTION_MRU: 390e022c2f0SKrzysztof Hałasa continue; /* MRU always OK and > 1500 bytes? */ 391e022c2f0SKrzysztof Hałasa 392e022c2f0SKrzysztof Hałasa case LCP_OPTION_ACCM: /* async control character map */ 39366d42ed8SDan Carpenter if (opt[1] < sizeof(valid_accm)) 39466d42ed8SDan Carpenter goto err_out; 395e022c2f0SKrzysztof Hałasa if (!memcmp(opt, valid_accm, 396e022c2f0SKrzysztof Hałasa sizeof(valid_accm))) 397e022c2f0SKrzysztof Hałasa continue; 398e022c2f0SKrzysztof Hałasa if (!rej_len) { /* NAK it */ 399e022c2f0SKrzysztof Hałasa memcpy(out + nak_len, valid_accm, 400e022c2f0SKrzysztof Hałasa sizeof(valid_accm)); 401e022c2f0SKrzysztof Hałasa nak_len += sizeof(valid_accm); 402e022c2f0SKrzysztof Hałasa continue; 403e022c2f0SKrzysztof Hałasa } 404e022c2f0SKrzysztof Hałasa break; 405e022c2f0SKrzysztof Hałasa case LCP_OPTION_MAGIC: 40666d42ed8SDan Carpenter if (len < 6) 40766d42ed8SDan Carpenter goto err_out; 408e022c2f0SKrzysztof Hałasa if (opt[1] != 6 || (!opt[2] && !opt[3] && 409e022c2f0SKrzysztof Hałasa !opt[4] && !opt[5])) 410e022c2f0SKrzysztof Hałasa break; /* reject invalid magic number */ 411e022c2f0SKrzysztof Hałasa continue; 412e022c2f0SKrzysztof Hałasa } 413e022c2f0SKrzysztof Hałasa /* reject this option */ 414e022c2f0SKrzysztof Hałasa memcpy(out + rej_len, opt, opt[1]); 415e022c2f0SKrzysztof Hałasa rej_len += opt[1]; 416e022c2f0SKrzysztof Hałasa } 417e022c2f0SKrzysztof Hałasa 418e022c2f0SKrzysztof Hałasa if (rej_len) 419e022c2f0SKrzysztof Hałasa ppp_cp_event(dev, pid, RCR_BAD, CP_CONF_REJ, id, rej_len, out); 420e022c2f0SKrzysztof Hałasa else if (nak_len) 421e022c2f0SKrzysztof Hałasa ppp_cp_event(dev, pid, RCR_BAD, CP_CONF_NAK, id, nak_len, out); 422e022c2f0SKrzysztof Hałasa else 42393bc9335SKrzysztof Hałasa ppp_cp_event(dev, pid, RCR_GOOD, CP_CONF_ACK, id, req_len, data); 424e022c2f0SKrzysztof Hałasa 425e022c2f0SKrzysztof Hałasa kfree(out); 42666d42ed8SDan Carpenter return; 42766d42ed8SDan Carpenter 42866d42ed8SDan Carpenter err_out: 42966d42ed8SDan Carpenter dev->stats.rx_errors++; 43066d42ed8SDan Carpenter kfree(out); 431e022c2f0SKrzysztof Hałasa } 432e022c2f0SKrzysztof Hałasa 433e022c2f0SKrzysztof Hałasa static int ppp_rx(struct sk_buff *skb) 434e022c2f0SKrzysztof Hałasa { 435e022c2f0SKrzysztof Hałasa struct hdlc_header *hdr = (struct hdlc_header*)skb->data; 436e022c2f0SKrzysztof Hałasa struct net_device *dev = skb->dev; 437e022c2f0SKrzysztof Hałasa struct ppp *ppp = get_ppp(dev); 438e022c2f0SKrzysztof Hałasa struct proto *proto; 439e022c2f0SKrzysztof Hałasa struct cp_header *cp; 440e022c2f0SKrzysztof Hałasa unsigned long flags; 441e022c2f0SKrzysztof Hałasa unsigned int len; 442e022c2f0SKrzysztof Hałasa u16 pid; 443e022c2f0SKrzysztof Hałasa #if DEBUG_CP 444e022c2f0SKrzysztof Hałasa int i; 445e022c2f0SKrzysztof Hałasa char *ptr; 446e022c2f0SKrzysztof Hałasa #endif 447e022c2f0SKrzysztof Hałasa 448e022c2f0SKrzysztof Hałasa spin_lock_irqsave(&ppp->lock, flags); 449e022c2f0SKrzysztof Hałasa /* Check HDLC header */ 450e022c2f0SKrzysztof Hałasa if (skb->len < sizeof(struct hdlc_header)) 451e022c2f0SKrzysztof Hałasa goto rx_error; 452af72868bSJohannes Berg cp = skb_pull(skb, sizeof(struct hdlc_header)); 453e022c2f0SKrzysztof Hałasa if (hdr->address != HDLC_ADDR_ALLSTATIONS || 454e022c2f0SKrzysztof Hałasa hdr->control != HDLC_CTRL_UI) 455e022c2f0SKrzysztof Hałasa goto rx_error; 456e022c2f0SKrzysztof Hałasa 457e022c2f0SKrzysztof Hałasa pid = ntohs(hdr->protocol); 458e022c2f0SKrzysztof Hałasa proto = get_proto(dev, pid); 459e022c2f0SKrzysztof Hałasa if (!proto) { 460e022c2f0SKrzysztof Hałasa if (ppp->protos[IDX_LCP].state == OPENED) 461e022c2f0SKrzysztof Hałasa ppp_tx_cp(dev, PID_LCP, LCP_PROTO_REJ, 462e022c2f0SKrzysztof Hałasa ++ppp->seq, skb->len + 2, &hdr->protocol); 463e022c2f0SKrzysztof Hałasa goto rx_error; 464e022c2f0SKrzysztof Hałasa } 465e022c2f0SKrzysztof Hałasa 466e022c2f0SKrzysztof Hałasa len = ntohs(cp->len); 467e022c2f0SKrzysztof Hałasa if (len < sizeof(struct cp_header) /* no complete CP header? */ || 468e022c2f0SKrzysztof Hałasa skb->len < len /* truncated packet? */) 469e022c2f0SKrzysztof Hałasa goto rx_error; 470e022c2f0SKrzysztof Hałasa skb_pull(skb, sizeof(struct cp_header)); 471e022c2f0SKrzysztof Hałasa len -= sizeof(struct cp_header); 472e022c2f0SKrzysztof Hałasa 473e022c2f0SKrzysztof Hałasa /* HDLC and CP headers stripped from skb */ 474e022c2f0SKrzysztof Hałasa #if DEBUG_CP 475e022c2f0SKrzysztof Hałasa if (cp->code < CP_CODES) 476e022c2f0SKrzysztof Hałasa sprintf(debug_buffer, "[%s id 0x%X]", code_names[cp->code], 477e022c2f0SKrzysztof Hałasa cp->id); 478e022c2f0SKrzysztof Hałasa else 479e022c2f0SKrzysztof Hałasa sprintf(debug_buffer, "[code %u id 0x%X]", cp->code, cp->id); 480e022c2f0SKrzysztof Hałasa ptr = debug_buffer + strlen(debug_buffer); 481e022c2f0SKrzysztof Hałasa for (i = 0; i < min_t(unsigned int, len, DEBUG_CP); i++) { 482e022c2f0SKrzysztof Hałasa sprintf(ptr, " %02X", skb->data[i]); 483e022c2f0SKrzysztof Hałasa ptr += strlen(ptr); 484e022c2f0SKrzysztof Hałasa } 485e022c2f0SKrzysztof Hałasa printk(KERN_DEBUG "%s: RX %s %s\n", dev->name, proto_name(pid), 486e022c2f0SKrzysztof Hałasa debug_buffer); 487e022c2f0SKrzysztof Hałasa #endif 488e022c2f0SKrzysztof Hałasa 489e022c2f0SKrzysztof Hałasa /* LCP only */ 490e022c2f0SKrzysztof Hałasa if (pid == PID_LCP) 491e022c2f0SKrzysztof Hałasa switch (cp->code) { 492e022c2f0SKrzysztof Hałasa case LCP_PROTO_REJ: 493e022c2f0SKrzysztof Hałasa pid = ntohs(*(__be16*)skb->data); 494e022c2f0SKrzysztof Hałasa if (pid == PID_LCP || pid == PID_IPCP || 495e022c2f0SKrzysztof Hałasa pid == PID_IPV6CP) 496e022c2f0SKrzysztof Hałasa ppp_cp_event(dev, pid, RXJ_BAD, 0, 0, 497e022c2f0SKrzysztof Hałasa 0, NULL); 498e022c2f0SKrzysztof Hałasa goto out; 499e022c2f0SKrzysztof Hałasa 500e022c2f0SKrzysztof Hałasa case LCP_ECHO_REQ: /* send Echo-Reply */ 501e022c2f0SKrzysztof Hałasa if (len >= 4 && proto->state == OPENED) 502e022c2f0SKrzysztof Hałasa ppp_tx_cp(dev, PID_LCP, LCP_ECHO_REPLY, 503e022c2f0SKrzysztof Hałasa cp->id, len - 4, skb->data + 4); 504e022c2f0SKrzysztof Hałasa goto out; 505e022c2f0SKrzysztof Hałasa 506e022c2f0SKrzysztof Hałasa case LCP_ECHO_REPLY: 507e022c2f0SKrzysztof Hałasa if (cp->id == ppp->echo_id) 508e022c2f0SKrzysztof Hałasa ppp->last_pong = jiffies; 509e022c2f0SKrzysztof Hałasa goto out; 510e022c2f0SKrzysztof Hałasa 511e022c2f0SKrzysztof Hałasa case LCP_DISC_REQ: /* discard */ 512e022c2f0SKrzysztof Hałasa goto out; 513e022c2f0SKrzysztof Hałasa } 514e022c2f0SKrzysztof Hałasa 515e022c2f0SKrzysztof Hałasa /* LCP, IPCP and IPV6CP */ 516e022c2f0SKrzysztof Hałasa switch (cp->code) { 517e022c2f0SKrzysztof Hałasa case CP_CONF_REQ: 518e022c2f0SKrzysztof Hałasa ppp_cp_parse_cr(dev, pid, cp->id, len, skb->data); 519c8d755b5SJesper Juhl break; 520e022c2f0SKrzysztof Hałasa 521e022c2f0SKrzysztof Hałasa case CP_CONF_ACK: 522e022c2f0SKrzysztof Hałasa if (cp->id == proto->cr_id) 523e022c2f0SKrzysztof Hałasa ppp_cp_event(dev, pid, RCA, 0, 0, 0, NULL); 524c8d755b5SJesper Juhl break; 525e022c2f0SKrzysztof Hałasa 526e022c2f0SKrzysztof Hałasa case CP_CONF_REJ: 527e022c2f0SKrzysztof Hałasa case CP_CONF_NAK: 528e022c2f0SKrzysztof Hałasa if (cp->id == proto->cr_id) 529e022c2f0SKrzysztof Hałasa ppp_cp_event(dev, pid, RCN, 0, 0, 0, NULL); 530c8d755b5SJesper Juhl break; 531e022c2f0SKrzysztof Hałasa 532e022c2f0SKrzysztof Hałasa case CP_TERM_REQ: 533e022c2f0SKrzysztof Hałasa ppp_cp_event(dev, pid, RTR, 0, cp->id, 0, NULL); 534c8d755b5SJesper Juhl break; 535e022c2f0SKrzysztof Hałasa 536e022c2f0SKrzysztof Hałasa case CP_TERM_ACK: 537e022c2f0SKrzysztof Hałasa ppp_cp_event(dev, pid, RTA, 0, 0, 0, NULL); 538c8d755b5SJesper Juhl break; 539e022c2f0SKrzysztof Hałasa 540e022c2f0SKrzysztof Hałasa case CP_CODE_REJ: 541e022c2f0SKrzysztof Hałasa ppp_cp_event(dev, pid, RXJ_BAD, 0, 0, 0, NULL); 542c8d755b5SJesper Juhl break; 543e022c2f0SKrzysztof Hałasa 544e022c2f0SKrzysztof Hałasa default: 545e022c2f0SKrzysztof Hałasa len += sizeof(struct cp_header); 546e022c2f0SKrzysztof Hałasa if (len > dev->mtu) 547e022c2f0SKrzysztof Hałasa len = dev->mtu; 548e022c2f0SKrzysztof Hałasa ppp_cp_event(dev, pid, RUC, 0, 0, len, cp); 549c8d755b5SJesper Juhl break; 550e022c2f0SKrzysztof Hałasa } 551e022c2f0SKrzysztof Hałasa goto out; 552e022c2f0SKrzysztof Hałasa 553e022c2f0SKrzysztof Hałasa rx_error: 554e022c2f0SKrzysztof Hałasa dev->stats.rx_errors++; 555e022c2f0SKrzysztof Hałasa out: 556e022c2f0SKrzysztof Hałasa spin_unlock_irqrestore(&ppp->lock, flags); 557e022c2f0SKrzysztof Hałasa dev_kfree_skb_any(skb); 558e022c2f0SKrzysztof Hałasa ppp_tx_flush(); 559e022c2f0SKrzysztof Hałasa return NET_RX_DROP; 560e022c2f0SKrzysztof Hałasa } 561e022c2f0SKrzysztof Hałasa 562e99e88a9SKees Cook static void ppp_timer(struct timer_list *t) 563e022c2f0SKrzysztof Hałasa { 564e99e88a9SKees Cook struct proto *proto = from_timer(proto, t, timer); 565e022c2f0SKrzysztof Hałasa struct ppp *ppp = get_ppp(proto->dev); 566e022c2f0SKrzysztof Hałasa unsigned long flags; 567e022c2f0SKrzysztof Hałasa 568e022c2f0SKrzysztof Hałasa spin_lock_irqsave(&ppp->lock, flags); 5691fef7359SXie He /* mod_timer could be called after we entered this function but 5701fef7359SXie He * before we got the lock. 5711fef7359SXie He */ 5721fef7359SXie He if (timer_pending(&proto->timer)) { 5731fef7359SXie He spin_unlock_irqrestore(&ppp->lock, flags); 5741fef7359SXie He return; 5751fef7359SXie He } 576e022c2f0SKrzysztof Hałasa switch (proto->state) { 577e022c2f0SKrzysztof Hałasa case STOPPING: 578e022c2f0SKrzysztof Hałasa case REQ_SENT: 579e022c2f0SKrzysztof Hałasa case ACK_RECV: 580e022c2f0SKrzysztof Hałasa case ACK_SENT: 581e022c2f0SKrzysztof Hałasa if (proto->restart_counter) { 582e022c2f0SKrzysztof Hałasa ppp_cp_event(proto->dev, proto->pid, TO_GOOD, 0, 0, 583e022c2f0SKrzysztof Hałasa 0, NULL); 584e022c2f0SKrzysztof Hałasa proto->restart_counter--; 585b6c3bad1SDenis Du } else if (netif_carrier_ok(proto->dev)) 586b6c3bad1SDenis Du ppp_cp_event(proto->dev, proto->pid, TO_GOOD, 0, 0, 587b6c3bad1SDenis Du 0, NULL); 588b6c3bad1SDenis Du else 589e022c2f0SKrzysztof Hałasa ppp_cp_event(proto->dev, proto->pid, TO_BAD, 0, 0, 590e022c2f0SKrzysztof Hałasa 0, NULL); 591e022c2f0SKrzysztof Hałasa break; 592e022c2f0SKrzysztof Hałasa 593e022c2f0SKrzysztof Hałasa case OPENED: 594e022c2f0SKrzysztof Hałasa if (proto->pid != PID_LCP) 595e022c2f0SKrzysztof Hałasa break; 596e022c2f0SKrzysztof Hałasa if (time_after(jiffies, ppp->last_pong + 597e022c2f0SKrzysztof Hałasa ppp->keepalive_timeout * HZ)) { 59812a3bfefSJoe Perches netdev_info(proto->dev, "Link down\n"); 599e022c2f0SKrzysztof Hałasa ppp_cp_event(proto->dev, PID_LCP, STOP, 0, 0, 0, NULL); 600e022c2f0SKrzysztof Hałasa ppp_cp_event(proto->dev, PID_LCP, START, 0, 0, 0, NULL); 601e022c2f0SKrzysztof Hałasa } else { /* send keep-alive packet */ 602e022c2f0SKrzysztof Hałasa ppp->echo_id = ++ppp->seq; 603e022c2f0SKrzysztof Hałasa ppp_tx_cp(proto->dev, PID_LCP, LCP_ECHO_REQ, 604e022c2f0SKrzysztof Hałasa ppp->echo_id, 0, NULL); 605e022c2f0SKrzysztof Hałasa proto->timer.expires = jiffies + 606e022c2f0SKrzysztof Hałasa ppp->keepalive_interval * HZ; 607e022c2f0SKrzysztof Hałasa add_timer(&proto->timer); 608e022c2f0SKrzysztof Hałasa } 609e022c2f0SKrzysztof Hałasa break; 610e022c2f0SKrzysztof Hałasa } 611e022c2f0SKrzysztof Hałasa spin_unlock_irqrestore(&ppp->lock, flags); 612e022c2f0SKrzysztof Hałasa ppp_tx_flush(); 613e022c2f0SKrzysztof Hałasa } 614e022c2f0SKrzysztof Hałasa 615e022c2f0SKrzysztof Hałasa static void ppp_start(struct net_device *dev) 616e022c2f0SKrzysztof Hałasa { 617e022c2f0SKrzysztof Hałasa struct ppp *ppp = get_ppp(dev); 618e022c2f0SKrzysztof Hałasa int i; 619e022c2f0SKrzysztof Hałasa 620e022c2f0SKrzysztof Hałasa for (i = 0; i < IDX_COUNT; i++) { 621e022c2f0SKrzysztof Hałasa struct proto *proto = &ppp->protos[i]; 622*2b57681fSPeng Li 623e022c2f0SKrzysztof Hałasa proto->dev = dev; 624e99e88a9SKees Cook timer_setup(&proto->timer, ppp_timer, 0); 625e022c2f0SKrzysztof Hałasa proto->state = CLOSED; 626e022c2f0SKrzysztof Hałasa } 627e022c2f0SKrzysztof Hałasa ppp->protos[IDX_LCP].pid = PID_LCP; 628e022c2f0SKrzysztof Hałasa ppp->protos[IDX_IPCP].pid = PID_IPCP; 629e022c2f0SKrzysztof Hałasa ppp->protos[IDX_IPV6CP].pid = PID_IPV6CP; 630e022c2f0SKrzysztof Hałasa 631e022c2f0SKrzysztof Hałasa ppp_cp_event(dev, PID_LCP, START, 0, 0, 0, NULL); 632e022c2f0SKrzysztof Hałasa } 633e022c2f0SKrzysztof Hałasa 634e022c2f0SKrzysztof Hałasa static void ppp_stop(struct net_device *dev) 635e022c2f0SKrzysztof Hałasa { 636e022c2f0SKrzysztof Hałasa ppp_cp_event(dev, PID_LCP, STOP, 0, 0, 0, NULL); 637e022c2f0SKrzysztof Hałasa } 6381da177e4SLinus Torvalds 63931f634a6SKrzysztof Halasa static void ppp_close(struct net_device *dev) 64031f634a6SKrzysztof Halasa { 64131f634a6SKrzysztof Halasa ppp_tx_flush(); 64231f634a6SKrzysztof Halasa } 64331f634a6SKrzysztof Halasa 644eb2a2fd9SKrzysztof Halasa static struct hdlc_proto proto = { 645e022c2f0SKrzysztof Hałasa .start = ppp_start, 646e022c2f0SKrzysztof Hałasa .stop = ppp_stop, 64731f634a6SKrzysztof Halasa .close = ppp_close, 648eb2a2fd9SKrzysztof Halasa .type_trans = ppp_type_trans, 649eb2a2fd9SKrzysztof Halasa .ioctl = ppp_ioctl, 650e022c2f0SKrzysztof Hałasa .netif_rx = ppp_rx, 651eb2a2fd9SKrzysztof Halasa .module = THIS_MODULE, 652eb2a2fd9SKrzysztof Halasa }; 653eb2a2fd9SKrzysztof Halasa 654e022c2f0SKrzysztof Hałasa static const struct header_ops ppp_header_ops = { 655e022c2f0SKrzysztof Hałasa .create = ppp_hard_header, 656e022c2f0SKrzysztof Hałasa }; 657eb2a2fd9SKrzysztof Halasa 658eb2a2fd9SKrzysztof Halasa static int ppp_ioctl(struct net_device *dev, struct ifreq *ifr) 6591da177e4SLinus Torvalds { 6601da177e4SLinus Torvalds hdlc_device *hdlc = dev_to_hdlc(dev); 661e022c2f0SKrzysztof Hałasa struct ppp *ppp; 6621da177e4SLinus Torvalds int result; 6631da177e4SLinus Torvalds 6641da177e4SLinus Torvalds switch (ifr->ifr_settings.type) { 6651da177e4SLinus Torvalds case IF_GET_PROTO: 666eb2a2fd9SKrzysztof Halasa if (dev_to_hdlc(dev)->proto != &proto) 667eb2a2fd9SKrzysztof Halasa return -EINVAL; 6681da177e4SLinus Torvalds ifr->ifr_settings.type = IF_PROTO_PPP; 6691da177e4SLinus Torvalds return 0; /* return protocol only, no settable parameters */ 6701da177e4SLinus Torvalds 6711da177e4SLinus Torvalds case IF_PROTO_PPP: 6721da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 6731da177e4SLinus Torvalds return -EPERM; 6741da177e4SLinus Torvalds 6751da177e4SLinus Torvalds if (dev->flags & IFF_UP) 6761da177e4SLinus Torvalds return -EBUSY; 6771da177e4SLinus Torvalds 6781da177e4SLinus Torvalds /* no settable parameters */ 6791da177e4SLinus Torvalds 6801da177e4SLinus Torvalds result = hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT); 6811da177e4SLinus Torvalds if (result) 6821da177e4SLinus Torvalds return result; 6831da177e4SLinus Torvalds 684e022c2f0SKrzysztof Hałasa result = attach_hdlc_protocol(dev, &proto, sizeof(struct ppp)); 685eb2a2fd9SKrzysztof Halasa if (result) 686eb2a2fd9SKrzysztof Halasa return result; 687e022c2f0SKrzysztof Hałasa 688e022c2f0SKrzysztof Hałasa ppp = get_ppp(dev); 689e022c2f0SKrzysztof Hałasa spin_lock_init(&ppp->lock); 690e022c2f0SKrzysztof Hałasa ppp->req_timeout = 2; 691e022c2f0SKrzysztof Hałasa ppp->cr_retries = 10; 692e022c2f0SKrzysztof Hałasa ppp->term_retries = 2; 693e022c2f0SKrzysztof Hałasa ppp->keepalive_interval = 10; 694e022c2f0SKrzysztof Hałasa ppp->keepalive_timeout = 60; 695e022c2f0SKrzysztof Hałasa 696e022c2f0SKrzysztof Hałasa dev->hard_header_len = sizeof(struct hdlc_header); 697e022c2f0SKrzysztof Hałasa dev->header_ops = &ppp_header_ops; 6981da177e4SLinus Torvalds dev->type = ARPHRD_PPP; 6992f8364a2SAndrew Lunn call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev); 700e022c2f0SKrzysztof Hałasa netif_dormant_on(dev); 7011da177e4SLinus Torvalds return 0; 7021da177e4SLinus Torvalds } 7031da177e4SLinus Torvalds 7041da177e4SLinus Torvalds return -EINVAL; 7051da177e4SLinus Torvalds } 706eb2a2fd9SKrzysztof Halasa 707eb2a2fd9SKrzysztof Halasa static int __init mod_init(void) 708eb2a2fd9SKrzysztof Halasa { 709e022c2f0SKrzysztof Hałasa skb_queue_head_init(&tx_queue); 710eb2a2fd9SKrzysztof Halasa register_hdlc_protocol(&proto); 711eb2a2fd9SKrzysztof Halasa return 0; 712eb2a2fd9SKrzysztof Halasa } 713eb2a2fd9SKrzysztof Halasa 714eb2a2fd9SKrzysztof Halasa static void __exit mod_exit(void) 715eb2a2fd9SKrzysztof Halasa { 716eb2a2fd9SKrzysztof Halasa unregister_hdlc_protocol(&proto); 717eb2a2fd9SKrzysztof Halasa } 718eb2a2fd9SKrzysztof Halasa 719eb2a2fd9SKrzysztof Halasa module_init(mod_init); 720eb2a2fd9SKrzysztof Halasa module_exit(mod_exit); 721eb2a2fd9SKrzysztof Halasa 722eb2a2fd9SKrzysztof Halasa MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>"); 723eb2a2fd9SKrzysztof Halasa MODULE_DESCRIPTION("PPP protocol support for generic HDLC"); 724eb2a2fd9SKrzysztof Halasa MODULE_LICENSE("GPL v2"); 725