11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Generic HDLC support routines for Linux 31da177e4SLinus Torvalds * Point-to-point protocol support 41da177e4SLinus Torvalds * 5e022c2f0SKrzysztof Hałasa * Copyright (C) 1999 - 2008 Krzysztof Halasa <khc@pm.waw.pl> 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify it 81da177e4SLinus Torvalds * under the terms of version 2 of the GNU General Public License 91da177e4SLinus Torvalds * as published by the Free Software Foundation. 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds #include <linux/errno.h> 131da177e4SLinus Torvalds #include <linux/hdlc.h> 144dfce407SKrzysztof Hałasa #include <linux/if_arp.h> 154dfce407SKrzysztof Hałasa #include <linux/inetdevice.h> 164dfce407SKrzysztof Hałasa #include <linux/init.h> 174dfce407SKrzysztof Hałasa #include <linux/kernel.h> 184dfce407SKrzysztof Hałasa #include <linux/module.h> 194dfce407SKrzysztof Hałasa #include <linux/pkt_sched.h> 204dfce407SKrzysztof Hałasa #include <linux/poll.h> 214dfce407SKrzysztof Hałasa #include <linux/skbuff.h> 224dfce407SKrzysztof Hałasa #include <linux/slab.h> 23e022c2f0SKrzysztof Hałasa #include <linux/spinlock.h> 24eb2a2fd9SKrzysztof Halasa 25e022c2f0SKrzysztof Hałasa #define DEBUG_CP 0 /* also bytes# to dump */ 26e022c2f0SKrzysztof Hałasa #define DEBUG_STATE 0 27e022c2f0SKrzysztof Hałasa #define DEBUG_HARD_HEADER 0 28e022c2f0SKrzysztof Hałasa 29e022c2f0SKrzysztof Hałasa #define HDLC_ADDR_ALLSTATIONS 0xFF 30e022c2f0SKrzysztof Hałasa #define HDLC_CTRL_UI 0x03 31e022c2f0SKrzysztof Hałasa 32e022c2f0SKrzysztof Hałasa #define PID_LCP 0xC021 33e022c2f0SKrzysztof Hałasa #define PID_IP 0x0021 34e022c2f0SKrzysztof Hałasa #define PID_IPCP 0x8021 35e022c2f0SKrzysztof Hałasa #define PID_IPV6 0x0057 36e022c2f0SKrzysztof Hałasa #define PID_IPV6CP 0x8057 37e022c2f0SKrzysztof Hałasa 38e022c2f0SKrzysztof Hałasa enum {IDX_LCP = 0, IDX_IPCP, IDX_IPV6CP, IDX_COUNT}; 39e022c2f0SKrzysztof Hałasa enum {CP_CONF_REQ = 1, CP_CONF_ACK, CP_CONF_NAK, CP_CONF_REJ, CP_TERM_REQ, 40e022c2f0SKrzysztof Hałasa CP_TERM_ACK, CP_CODE_REJ, LCP_PROTO_REJ, LCP_ECHO_REQ, LCP_ECHO_REPLY, 41e022c2f0SKrzysztof Hałasa LCP_DISC_REQ, CP_CODES}; 42e022c2f0SKrzysztof Hałasa #if DEBUG_CP 43e022c2f0SKrzysztof Hałasa static const char *const code_names[CP_CODES] = { 44e022c2f0SKrzysztof Hałasa "0", "ConfReq", "ConfAck", "ConfNak", "ConfRej", "TermReq", 45e022c2f0SKrzysztof Hałasa "TermAck", "CodeRej", "ProtoRej", "EchoReq", "EchoReply", "Discard" 46eb2a2fd9SKrzysztof Halasa }; 47e022c2f0SKrzysztof Hałasa static char debug_buffer[64 + 3 * DEBUG_CP]; 48e022c2f0SKrzysztof Hałasa #endif 49e022c2f0SKrzysztof Hałasa 50e022c2f0SKrzysztof Hałasa enum {LCP_OPTION_MRU = 1, LCP_OPTION_ACCM, LCP_OPTION_MAGIC = 5}; 51e022c2f0SKrzysztof Hałasa 52e022c2f0SKrzysztof Hałasa struct hdlc_header { 53e022c2f0SKrzysztof Hałasa u8 address; 54e022c2f0SKrzysztof Hałasa u8 control; 55e022c2f0SKrzysztof Hałasa __be16 protocol; 56e022c2f0SKrzysztof Hałasa }; 57e022c2f0SKrzysztof Hałasa 58e022c2f0SKrzysztof Hałasa struct cp_header { 59e022c2f0SKrzysztof Hałasa u8 code; 60e022c2f0SKrzysztof Hałasa u8 id; 61e022c2f0SKrzysztof Hałasa __be16 len; 62e022c2f0SKrzysztof Hałasa }; 63e022c2f0SKrzysztof Hałasa 64e022c2f0SKrzysztof Hałasa 65e022c2f0SKrzysztof Hałasa struct proto { 66e022c2f0SKrzysztof Hałasa struct net_device *dev; 67e022c2f0SKrzysztof Hałasa struct timer_list timer; 68e022c2f0SKrzysztof Hałasa unsigned long timeout; 69e022c2f0SKrzysztof Hałasa u16 pid; /* protocol ID */ 70e022c2f0SKrzysztof Hałasa u8 state; 71e022c2f0SKrzysztof Hałasa u8 cr_id; /* ID of last Configuration-Request */ 72e022c2f0SKrzysztof Hałasa u8 restart_counter; 73e022c2f0SKrzysztof Hałasa }; 74e022c2f0SKrzysztof Hałasa 75e022c2f0SKrzysztof Hałasa struct ppp { 76e022c2f0SKrzysztof Hałasa struct proto protos[IDX_COUNT]; 77e022c2f0SKrzysztof Hałasa spinlock_t lock; 78e022c2f0SKrzysztof Hałasa unsigned long last_pong; 79e022c2f0SKrzysztof Hałasa unsigned int req_timeout, cr_retries, term_retries; 80e022c2f0SKrzysztof Hałasa unsigned int keepalive_interval, keepalive_timeout; 81e022c2f0SKrzysztof Hałasa u8 seq; /* local sequence number for requests */ 82e022c2f0SKrzysztof Hałasa u8 echo_id; /* ID of last Echo-Request (LCP) */ 83e022c2f0SKrzysztof Hałasa }; 84e022c2f0SKrzysztof Hałasa 85e022c2f0SKrzysztof Hałasa enum {CLOSED = 0, STOPPED, STOPPING, REQ_SENT, ACK_RECV, ACK_SENT, OPENED, 86e022c2f0SKrzysztof Hałasa STATES, STATE_MASK = 0xF}; 87e022c2f0SKrzysztof Hałasa enum {START = 0, STOP, TO_GOOD, TO_BAD, RCR_GOOD, RCR_BAD, RCA, RCN, RTR, RTA, 88e022c2f0SKrzysztof Hałasa RUC, RXJ_GOOD, RXJ_BAD, EVENTS}; 89e022c2f0SKrzysztof Hałasa enum {INV = 0x10, IRC = 0x20, ZRC = 0x40, SCR = 0x80, SCA = 0x100, 90e022c2f0SKrzysztof Hałasa SCN = 0x200, STR = 0x400, STA = 0x800, SCJ = 0x1000}; 91e022c2f0SKrzysztof Hałasa 92e022c2f0SKrzysztof Hałasa #if DEBUG_STATE 93e022c2f0SKrzysztof Hałasa static const char *const state_names[STATES] = { 94e022c2f0SKrzysztof Hałasa "Closed", "Stopped", "Stopping", "ReqSent", "AckRecv", "AckSent", 95e022c2f0SKrzysztof Hałasa "Opened" 96e022c2f0SKrzysztof Hałasa }; 97e022c2f0SKrzysztof Hałasa static const char *const event_names[EVENTS] = { 98e022c2f0SKrzysztof Hałasa "Start", "Stop", "TO+", "TO-", "RCR+", "RCR-", "RCA", "RCN", 99e022c2f0SKrzysztof Hałasa "RTR", "RTA", "RUC", "RXJ+", "RXJ-" 100e022c2f0SKrzysztof Hałasa }; 101e022c2f0SKrzysztof Hałasa #endif 102e022c2f0SKrzysztof Hałasa 103e022c2f0SKrzysztof Hałasa static struct sk_buff_head tx_queue; /* used when holding the spin lock */ 104eb2a2fd9SKrzysztof Halasa 105eb2a2fd9SKrzysztof Halasa static int ppp_ioctl(struct net_device *dev, struct ifreq *ifr); 106eb2a2fd9SKrzysztof Halasa 107e022c2f0SKrzysztof Hałasa static inline struct ppp* get_ppp(struct net_device *dev) 108eb2a2fd9SKrzysztof Halasa { 109e022c2f0SKrzysztof Hałasa return (struct ppp *)dev_to_hdlc(dev)->state; 110eb2a2fd9SKrzysztof Halasa } 1111da177e4SLinus Torvalds 112e022c2f0SKrzysztof Hałasa static inline struct proto* get_proto(struct net_device *dev, u16 pid) 1131da177e4SLinus Torvalds { 114e022c2f0SKrzysztof Hałasa struct ppp *ppp = get_ppp(dev); 1151da177e4SLinus Torvalds 116e022c2f0SKrzysztof Hałasa switch (pid) { 117e022c2f0SKrzysztof Hałasa case PID_LCP: 118e022c2f0SKrzysztof Hałasa return &ppp->protos[IDX_LCP]; 119e022c2f0SKrzysztof Hałasa case PID_IPCP: 120e022c2f0SKrzysztof Hałasa return &ppp->protos[IDX_IPCP]; 121e022c2f0SKrzysztof Hałasa case PID_IPV6CP: 122e022c2f0SKrzysztof Hałasa return &ppp->protos[IDX_IPV6CP]; 123e022c2f0SKrzysztof Hałasa default: 124e022c2f0SKrzysztof Hałasa return NULL; 125e022c2f0SKrzysztof Hałasa } 1261da177e4SLinus Torvalds } 1271da177e4SLinus Torvalds 128e022c2f0SKrzysztof Hałasa static inline const char* proto_name(u16 pid) 1291da177e4SLinus Torvalds { 130e022c2f0SKrzysztof Hałasa switch (pid) { 131e022c2f0SKrzysztof Hałasa case PID_LCP: 132e022c2f0SKrzysztof Hałasa return "LCP"; 133e022c2f0SKrzysztof Hałasa case PID_IPCP: 134e022c2f0SKrzysztof Hałasa return "IPCP"; 135e022c2f0SKrzysztof Hałasa case PID_IPV6CP: 136e022c2f0SKrzysztof Hałasa return "IPV6CP"; 137e022c2f0SKrzysztof Hałasa default: 138e022c2f0SKrzysztof Hałasa return NULL; 1391da177e4SLinus Torvalds } 140e022c2f0SKrzysztof Hałasa } 1411da177e4SLinus Torvalds 142ab611487SAlexey Dobriyan static __be16 ppp_type_trans(struct sk_buff *skb, struct net_device *dev) 1431da177e4SLinus Torvalds { 144e022c2f0SKrzysztof Hałasa struct hdlc_header *data = (struct hdlc_header*)skb->data; 145e022c2f0SKrzysztof Hałasa 146e022c2f0SKrzysztof Hałasa if (skb->len < sizeof(struct hdlc_header)) 147e022c2f0SKrzysztof Hałasa return htons(ETH_P_HDLC); 148e022c2f0SKrzysztof Hałasa if (data->address != HDLC_ADDR_ALLSTATIONS || 149e022c2f0SKrzysztof Hałasa data->control != HDLC_CTRL_UI) 150e022c2f0SKrzysztof Hałasa return htons(ETH_P_HDLC); 151e022c2f0SKrzysztof Hałasa 152e022c2f0SKrzysztof Hałasa switch (data->protocol) { 15309640e63SHarvey Harrison case cpu_to_be16(PID_IP): 154e022c2f0SKrzysztof Hałasa skb_pull(skb, sizeof(struct hdlc_header)); 155e022c2f0SKrzysztof Hałasa return htons(ETH_P_IP); 156e022c2f0SKrzysztof Hałasa 15709640e63SHarvey Harrison case cpu_to_be16(PID_IPV6): 158e022c2f0SKrzysztof Hałasa skb_pull(skb, sizeof(struct hdlc_header)); 159e022c2f0SKrzysztof Hałasa return htons(ETH_P_IPV6); 160e022c2f0SKrzysztof Hałasa 161e022c2f0SKrzysztof Hałasa default: 162e022c2f0SKrzysztof Hałasa return htons(ETH_P_HDLC); 163e022c2f0SKrzysztof Hałasa } 1641da177e4SLinus Torvalds } 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds 167e022c2f0SKrzysztof Hałasa static int ppp_hard_header(struct sk_buff *skb, struct net_device *dev, 168e022c2f0SKrzysztof Hałasa u16 type, const void *daddr, const void *saddr, 169e022c2f0SKrzysztof Hałasa unsigned int len) 170e022c2f0SKrzysztof Hałasa { 171e022c2f0SKrzysztof Hałasa struct hdlc_header *data; 172e022c2f0SKrzysztof Hałasa #if DEBUG_HARD_HEADER 173e022c2f0SKrzysztof Hałasa printk(KERN_DEBUG "%s: ppp_hard_header() called\n", dev->name); 174e022c2f0SKrzysztof Hałasa #endif 175e022c2f0SKrzysztof Hałasa 176e022c2f0SKrzysztof Hałasa skb_push(skb, sizeof(struct hdlc_header)); 177e022c2f0SKrzysztof Hałasa data = (struct hdlc_header*)skb->data; 178e022c2f0SKrzysztof Hałasa 179e022c2f0SKrzysztof Hałasa data->address = HDLC_ADDR_ALLSTATIONS; 180e022c2f0SKrzysztof Hałasa data->control = HDLC_CTRL_UI; 181e022c2f0SKrzysztof Hałasa switch (type) { 182e022c2f0SKrzysztof Hałasa case ETH_P_IP: 183e022c2f0SKrzysztof Hałasa data->protocol = htons(PID_IP); 184e022c2f0SKrzysztof Hałasa break; 185e022c2f0SKrzysztof Hałasa case ETH_P_IPV6: 186e022c2f0SKrzysztof Hałasa data->protocol = htons(PID_IPV6); 187e022c2f0SKrzysztof Hałasa break; 188e022c2f0SKrzysztof Hałasa case PID_LCP: 189e022c2f0SKrzysztof Hałasa case PID_IPCP: 190e022c2f0SKrzysztof Hałasa case PID_IPV6CP: 191e022c2f0SKrzysztof Hałasa data->protocol = htons(type); 192e022c2f0SKrzysztof Hałasa break; 193e022c2f0SKrzysztof Hałasa default: /* unknown protocol */ 194e022c2f0SKrzysztof Hałasa data->protocol = 0; 195e022c2f0SKrzysztof Hałasa } 196e022c2f0SKrzysztof Hałasa return sizeof(struct hdlc_header); 197e022c2f0SKrzysztof Hałasa } 198e022c2f0SKrzysztof Hałasa 199e022c2f0SKrzysztof Hałasa 200e022c2f0SKrzysztof Hałasa static void ppp_tx_flush(void) 201e022c2f0SKrzysztof Hałasa { 202e022c2f0SKrzysztof Hałasa struct sk_buff *skb; 203e022c2f0SKrzysztof Hałasa while ((skb = skb_dequeue(&tx_queue)) != NULL) 204e022c2f0SKrzysztof Hałasa dev_queue_xmit(skb); 205e022c2f0SKrzysztof Hałasa } 206e022c2f0SKrzysztof Hałasa 207e022c2f0SKrzysztof Hałasa static void ppp_tx_cp(struct net_device *dev, u16 pid, u8 code, 208e022c2f0SKrzysztof Hałasa u8 id, unsigned int len, const void *data) 209e022c2f0SKrzysztof Hałasa { 210e022c2f0SKrzysztof Hałasa struct sk_buff *skb; 211e022c2f0SKrzysztof Hałasa struct cp_header *cp; 212e022c2f0SKrzysztof Hałasa unsigned int magic_len = 0; 213e022c2f0SKrzysztof Hałasa static u32 magic; 214e022c2f0SKrzysztof Hałasa 215e022c2f0SKrzysztof Hałasa #if DEBUG_CP 216e022c2f0SKrzysztof Hałasa int i; 217e022c2f0SKrzysztof Hałasa char *ptr; 218e022c2f0SKrzysztof Hałasa #endif 219e022c2f0SKrzysztof Hałasa 220e022c2f0SKrzysztof Hałasa if (pid == PID_LCP && (code == LCP_ECHO_REQ || code == LCP_ECHO_REPLY)) 221e022c2f0SKrzysztof Hałasa magic_len = sizeof(magic); 222e022c2f0SKrzysztof Hałasa 223e022c2f0SKrzysztof Hałasa skb = dev_alloc_skb(sizeof(struct hdlc_header) + 224e022c2f0SKrzysztof Hałasa sizeof(struct cp_header) + magic_len + len); 225e022c2f0SKrzysztof Hałasa if (!skb) { 22612a3bfefSJoe Perches netdev_warn(dev, "out of memory in ppp_tx_cp()\n"); 227e022c2f0SKrzysztof Hałasa return; 228e022c2f0SKrzysztof Hałasa } 229e022c2f0SKrzysztof Hałasa skb_reserve(skb, sizeof(struct hdlc_header)); 230e022c2f0SKrzysztof Hałasa 231e022c2f0SKrzysztof Hałasa cp = (struct cp_header *)skb_put(skb, sizeof(struct cp_header)); 232e022c2f0SKrzysztof Hałasa cp->code = code; 233e022c2f0SKrzysztof Hałasa cp->id = id; 234e022c2f0SKrzysztof Hałasa cp->len = htons(sizeof(struct cp_header) + magic_len + len); 235e022c2f0SKrzysztof Hałasa 236e022c2f0SKrzysztof Hałasa if (magic_len) 237e022c2f0SKrzysztof Hałasa memcpy(skb_put(skb, magic_len), &magic, magic_len); 238e022c2f0SKrzysztof Hałasa if (len) 239e022c2f0SKrzysztof Hałasa memcpy(skb_put(skb, len), data, len); 240e022c2f0SKrzysztof Hałasa 241e022c2f0SKrzysztof Hałasa #if DEBUG_CP 242e022c2f0SKrzysztof Hałasa BUG_ON(code >= CP_CODES); 243e022c2f0SKrzysztof Hałasa ptr = debug_buffer; 244e022c2f0SKrzysztof Hałasa *ptr = '\x0'; 245e022c2f0SKrzysztof Hałasa for (i = 0; i < min_t(unsigned int, magic_len + len, DEBUG_CP); i++) { 246e022c2f0SKrzysztof Hałasa sprintf(ptr, " %02X", skb->data[sizeof(struct cp_header) + i]); 247e022c2f0SKrzysztof Hałasa ptr += strlen(ptr); 248e022c2f0SKrzysztof Hałasa } 249e022c2f0SKrzysztof Hałasa printk(KERN_DEBUG "%s: TX %s [%s id 0x%X]%s\n", dev->name, 250e022c2f0SKrzysztof Hałasa proto_name(pid), code_names[code], id, debug_buffer); 251e022c2f0SKrzysztof Hałasa #endif 252e022c2f0SKrzysztof Hałasa 253e022c2f0SKrzysztof Hałasa ppp_hard_header(skb, dev, pid, NULL, NULL, 0); 254e022c2f0SKrzysztof Hałasa 255e022c2f0SKrzysztof Hałasa skb->priority = TC_PRIO_CONTROL; 256e022c2f0SKrzysztof Hałasa skb->dev = dev; 257e022c2f0SKrzysztof Hałasa skb_reset_network_header(skb); 258e022c2f0SKrzysztof Hałasa skb_queue_tail(&tx_queue, skb); 259e022c2f0SKrzysztof Hałasa } 260e022c2f0SKrzysztof Hałasa 261e022c2f0SKrzysztof Hałasa 262e022c2f0SKrzysztof Hałasa /* State transition table (compare STD-51) 263e022c2f0SKrzysztof Hałasa Events Actions 264e022c2f0SKrzysztof Hałasa TO+ = Timeout with counter > 0 irc = Initialize-Restart-Count 265e022c2f0SKrzysztof Hałasa TO- = Timeout with counter expired zrc = Zero-Restart-Count 266e022c2f0SKrzysztof Hałasa 267e022c2f0SKrzysztof Hałasa RCR+ = Receive-Configure-Request (Good) scr = Send-Configure-Request 268e022c2f0SKrzysztof Hałasa RCR- = Receive-Configure-Request (Bad) 269e022c2f0SKrzysztof Hałasa RCA = Receive-Configure-Ack sca = Send-Configure-Ack 270e022c2f0SKrzysztof Hałasa RCN = Receive-Configure-Nak/Rej scn = Send-Configure-Nak/Rej 271e022c2f0SKrzysztof Hałasa 272e022c2f0SKrzysztof Hałasa RTR = Receive-Terminate-Request str = Send-Terminate-Request 273e022c2f0SKrzysztof Hałasa RTA = Receive-Terminate-Ack sta = Send-Terminate-Ack 274e022c2f0SKrzysztof Hałasa 275e022c2f0SKrzysztof Hałasa RUC = Receive-Unknown-Code scj = Send-Code-Reject 276e022c2f0SKrzysztof Hałasa RXJ+ = Receive-Code-Reject (permitted) 277e022c2f0SKrzysztof Hałasa or Receive-Protocol-Reject 278e022c2f0SKrzysztof Hałasa RXJ- = Receive-Code-Reject (catastrophic) 279e022c2f0SKrzysztof Hałasa or Receive-Protocol-Reject 280e022c2f0SKrzysztof Hałasa */ 281e022c2f0SKrzysztof Hałasa static int cp_table[EVENTS][STATES] = { 282e022c2f0SKrzysztof Hałasa /* CLOSED STOPPED STOPPING REQ_SENT ACK_RECV ACK_SENT OPENED 283e022c2f0SKrzysztof Hałasa 0 1 2 3 4 5 6 */ 284e022c2f0SKrzysztof Hałasa {IRC|SCR|3, INV , INV , INV , INV , INV , INV }, /* START */ 285e022c2f0SKrzysztof Hałasa { INV , 0 , 0 , 0 , 0 , 0 , 0 }, /* STOP */ 286e022c2f0SKrzysztof Hałasa { INV , INV ,STR|2, SCR|3 ,SCR|3, SCR|5 , INV }, /* TO+ */ 287e022c2f0SKrzysztof Hałasa { INV , INV , 1 , 1 , 1 , 1 , INV }, /* TO- */ 288e022c2f0SKrzysztof Hałasa { STA|0 ,IRC|SCR|SCA|5, 2 , SCA|5 ,SCA|6, SCA|5 ,SCR|SCA|5}, /* RCR+ */ 289e022c2f0SKrzysztof Hałasa { STA|0 ,IRC|SCR|SCN|3, 2 , SCN|3 ,SCN|4, SCN|3 ,SCR|SCN|3}, /* RCR- */ 290e022c2f0SKrzysztof Hałasa { STA|0 , STA|1 , 2 , IRC|4 ,SCR|3, 6 , SCR|3 }, /* RCA */ 291e022c2f0SKrzysztof Hałasa { STA|0 , STA|1 , 2 ,IRC|SCR|3,SCR|3,IRC|SCR|5, SCR|3 }, /* RCN */ 292e022c2f0SKrzysztof Hałasa { STA|0 , STA|1 ,STA|2, STA|3 ,STA|3, STA|3 ,ZRC|STA|2}, /* RTR */ 293e022c2f0SKrzysztof Hałasa { 0 , 1 , 1 , 3 , 3 , 5 , SCR|3 }, /* RTA */ 294e022c2f0SKrzysztof Hałasa { SCJ|0 , SCJ|1 ,SCJ|2, SCJ|3 ,SCJ|4, SCJ|5 , SCJ|6 }, /* RUC */ 295e022c2f0SKrzysztof Hałasa { 0 , 1 , 2 , 3 , 3 , 5 , 6 }, /* RXJ+ */ 296e022c2f0SKrzysztof Hałasa { 0 , 1 , 1 , 1 , 1 , 1 ,IRC|STR|2}, /* RXJ- */ 297e022c2f0SKrzysztof Hałasa }; 298e022c2f0SKrzysztof Hałasa 299e022c2f0SKrzysztof Hałasa 300e022c2f0SKrzysztof Hałasa /* SCA: RCR+ must supply id, len and data 301e022c2f0SKrzysztof Hałasa SCN: RCR- must supply code, id, len and data 302e022c2f0SKrzysztof Hałasa STA: RTR must supply id 303e022c2f0SKrzysztof Hałasa SCJ: RUC must supply CP packet len and data */ 304e022c2f0SKrzysztof Hałasa static void ppp_cp_event(struct net_device *dev, u16 pid, u16 event, u8 code, 30593bc9335SKrzysztof Hałasa u8 id, unsigned int len, const void *data) 306e022c2f0SKrzysztof Hałasa { 307e022c2f0SKrzysztof Hałasa int old_state, action; 308e022c2f0SKrzysztof Hałasa struct ppp *ppp = get_ppp(dev); 309e022c2f0SKrzysztof Hałasa struct proto *proto = get_proto(dev, pid); 310e022c2f0SKrzysztof Hałasa 311e022c2f0SKrzysztof Hałasa old_state = proto->state; 312e022c2f0SKrzysztof Hałasa BUG_ON(old_state >= STATES); 313e022c2f0SKrzysztof Hałasa BUG_ON(event >= EVENTS); 314e022c2f0SKrzysztof Hałasa 315e022c2f0SKrzysztof Hałasa #if DEBUG_STATE 316e022c2f0SKrzysztof Hałasa printk(KERN_DEBUG "%s: %s ppp_cp_event(%s) %s ...\n", dev->name, 317e022c2f0SKrzysztof Hałasa proto_name(pid), event_names[event], state_names[proto->state]); 318e022c2f0SKrzysztof Hałasa #endif 319e022c2f0SKrzysztof Hałasa 320e022c2f0SKrzysztof Hałasa action = cp_table[event][old_state]; 321e022c2f0SKrzysztof Hałasa 322e022c2f0SKrzysztof Hałasa proto->state = action & STATE_MASK; 323e022c2f0SKrzysztof Hałasa if (action & (SCR | STR)) /* set Configure-Req/Terminate-Req timer */ 324e022c2f0SKrzysztof Hałasa mod_timer(&proto->timer, proto->timeout = 325e022c2f0SKrzysztof Hałasa jiffies + ppp->req_timeout * HZ); 326e022c2f0SKrzysztof Hałasa if (action & ZRC) 327e022c2f0SKrzysztof Hałasa proto->restart_counter = 0; 328e022c2f0SKrzysztof Hałasa if (action & IRC) 329e022c2f0SKrzysztof Hałasa proto->restart_counter = (proto->state == STOPPING) ? 330e022c2f0SKrzysztof Hałasa ppp->term_retries : ppp->cr_retries; 331e022c2f0SKrzysztof Hałasa 332e022c2f0SKrzysztof Hałasa if (action & SCR) /* send Configure-Request */ 333e022c2f0SKrzysztof Hałasa ppp_tx_cp(dev, pid, CP_CONF_REQ, proto->cr_id = ++ppp->seq, 334e022c2f0SKrzysztof Hałasa 0, NULL); 335e022c2f0SKrzysztof Hałasa if (action & SCA) /* send Configure-Ack */ 336e022c2f0SKrzysztof Hałasa ppp_tx_cp(dev, pid, CP_CONF_ACK, id, len, data); 337e022c2f0SKrzysztof Hałasa if (action & SCN) /* send Configure-Nak/Reject */ 338e022c2f0SKrzysztof Hałasa ppp_tx_cp(dev, pid, code, id, len, data); 339e022c2f0SKrzysztof Hałasa if (action & STR) /* send Terminate-Request */ 340e022c2f0SKrzysztof Hałasa ppp_tx_cp(dev, pid, CP_TERM_REQ, ++ppp->seq, 0, NULL); 341e022c2f0SKrzysztof Hałasa if (action & STA) /* send Terminate-Ack */ 342e022c2f0SKrzysztof Hałasa ppp_tx_cp(dev, pid, CP_TERM_ACK, id, 0, NULL); 343e022c2f0SKrzysztof Hałasa if (action & SCJ) /* send Code-Reject */ 344e022c2f0SKrzysztof Hałasa ppp_tx_cp(dev, pid, CP_CODE_REJ, ++ppp->seq, len, data); 345e022c2f0SKrzysztof Hałasa 346e022c2f0SKrzysztof Hałasa if (old_state != OPENED && proto->state == OPENED) { 34712a3bfefSJoe Perches netdev_info(dev, "%s up\n", proto_name(pid)); 348e022c2f0SKrzysztof Hałasa if (pid == PID_LCP) { 349e022c2f0SKrzysztof Hałasa netif_dormant_off(dev); 350e022c2f0SKrzysztof Hałasa ppp_cp_event(dev, PID_IPCP, START, 0, 0, 0, NULL); 351e022c2f0SKrzysztof Hałasa ppp_cp_event(dev, PID_IPV6CP, START, 0, 0, 0, NULL); 352e022c2f0SKrzysztof Hałasa ppp->last_pong = jiffies; 353e022c2f0SKrzysztof Hałasa mod_timer(&proto->timer, proto->timeout = 354e022c2f0SKrzysztof Hałasa jiffies + ppp->keepalive_interval * HZ); 355e022c2f0SKrzysztof Hałasa } 356e022c2f0SKrzysztof Hałasa } 357e022c2f0SKrzysztof Hałasa if (old_state == OPENED && proto->state != OPENED) { 35812a3bfefSJoe Perches netdev_info(dev, "%s down\n", proto_name(pid)); 359e022c2f0SKrzysztof Hałasa if (pid == PID_LCP) { 360e022c2f0SKrzysztof Hałasa netif_dormant_on(dev); 361e022c2f0SKrzysztof Hałasa ppp_cp_event(dev, PID_IPCP, STOP, 0, 0, 0, NULL); 362e022c2f0SKrzysztof Hałasa ppp_cp_event(dev, PID_IPV6CP, STOP, 0, 0, 0, NULL); 363e022c2f0SKrzysztof Hałasa } 364e022c2f0SKrzysztof Hałasa } 365e022c2f0SKrzysztof Hałasa if (old_state != CLOSED && proto->state == CLOSED) 366e022c2f0SKrzysztof Hałasa del_timer(&proto->timer); 367e022c2f0SKrzysztof Hałasa 368e022c2f0SKrzysztof Hałasa #if DEBUG_STATE 369e022c2f0SKrzysztof Hałasa printk(KERN_DEBUG "%s: %s ppp_cp_event(%s) ... %s\n", dev->name, 370e022c2f0SKrzysztof Hałasa proto_name(pid), event_names[event], state_names[proto->state]); 371e022c2f0SKrzysztof Hałasa #endif 372e022c2f0SKrzysztof Hałasa } 373e022c2f0SKrzysztof Hałasa 374e022c2f0SKrzysztof Hałasa 375e022c2f0SKrzysztof Hałasa static void ppp_cp_parse_cr(struct net_device *dev, u16 pid, u8 id, 37693bc9335SKrzysztof Hałasa unsigned int req_len, const u8 *data) 377e022c2f0SKrzysztof Hałasa { 378e022c2f0SKrzysztof Hałasa static u8 const valid_accm[6] = { LCP_OPTION_ACCM, 6, 0, 0, 0, 0 }; 37993bc9335SKrzysztof Hałasa const u8 *opt; 38093bc9335SKrzysztof Hałasa u8 *out; 38193bc9335SKrzysztof Hałasa unsigned int len = req_len, nak_len = 0, rej_len = 0; 382e022c2f0SKrzysztof Hałasa 383e022c2f0SKrzysztof Hałasa if (!(out = kmalloc(len, GFP_ATOMIC))) { 384e022c2f0SKrzysztof Hałasa dev->stats.rx_dropped++; 385e022c2f0SKrzysztof Hałasa return; /* out of memory, ignore CR packet */ 386e022c2f0SKrzysztof Hałasa } 387e022c2f0SKrzysztof Hałasa 388e022c2f0SKrzysztof Hałasa for (opt = data; len; len -= opt[1], opt += opt[1]) { 389e022c2f0SKrzysztof Hałasa if (len < 2 || len < opt[1]) { 390e022c2f0SKrzysztof Hałasa dev->stats.rx_errors++; 391966a5d1bSJulia Lawall kfree(out); 392e022c2f0SKrzysztof Hałasa return; /* bad packet, drop silently */ 393e022c2f0SKrzysztof Hałasa } 394e022c2f0SKrzysztof Hałasa 395e022c2f0SKrzysztof Hałasa if (pid == PID_LCP) 396e022c2f0SKrzysztof Hałasa switch (opt[0]) { 397e022c2f0SKrzysztof Hałasa case LCP_OPTION_MRU: 398e022c2f0SKrzysztof Hałasa continue; /* MRU always OK and > 1500 bytes? */ 399e022c2f0SKrzysztof Hałasa 400e022c2f0SKrzysztof Hałasa case LCP_OPTION_ACCM: /* async control character map */ 401e022c2f0SKrzysztof Hałasa if (!memcmp(opt, valid_accm, 402e022c2f0SKrzysztof Hałasa sizeof(valid_accm))) 403e022c2f0SKrzysztof Hałasa continue; 404e022c2f0SKrzysztof Hałasa if (!rej_len) { /* NAK it */ 405e022c2f0SKrzysztof Hałasa memcpy(out + nak_len, valid_accm, 406e022c2f0SKrzysztof Hałasa sizeof(valid_accm)); 407e022c2f0SKrzysztof Hałasa nak_len += sizeof(valid_accm); 408e022c2f0SKrzysztof Hałasa continue; 409e022c2f0SKrzysztof Hałasa } 410e022c2f0SKrzysztof Hałasa break; 411e022c2f0SKrzysztof Hałasa case LCP_OPTION_MAGIC: 412e022c2f0SKrzysztof Hałasa if (opt[1] != 6 || (!opt[2] && !opt[3] && 413e022c2f0SKrzysztof Hałasa !opt[4] && !opt[5])) 414e022c2f0SKrzysztof Hałasa break; /* reject invalid magic number */ 415e022c2f0SKrzysztof Hałasa continue; 416e022c2f0SKrzysztof Hałasa } 417e022c2f0SKrzysztof Hałasa /* reject this option */ 418e022c2f0SKrzysztof Hałasa memcpy(out + rej_len, opt, opt[1]); 419e022c2f0SKrzysztof Hałasa rej_len += opt[1]; 420e022c2f0SKrzysztof Hałasa } 421e022c2f0SKrzysztof Hałasa 422e022c2f0SKrzysztof Hałasa if (rej_len) 423e022c2f0SKrzysztof Hałasa ppp_cp_event(dev, pid, RCR_BAD, CP_CONF_REJ, id, rej_len, out); 424e022c2f0SKrzysztof Hałasa else if (nak_len) 425e022c2f0SKrzysztof Hałasa ppp_cp_event(dev, pid, RCR_BAD, CP_CONF_NAK, id, nak_len, out); 426e022c2f0SKrzysztof Hałasa else 42793bc9335SKrzysztof Hałasa ppp_cp_event(dev, pid, RCR_GOOD, CP_CONF_ACK, id, req_len, data); 428e022c2f0SKrzysztof Hałasa 429e022c2f0SKrzysztof Hałasa kfree(out); 430e022c2f0SKrzysztof Hałasa } 431e022c2f0SKrzysztof Hałasa 432e022c2f0SKrzysztof Hałasa static int ppp_rx(struct sk_buff *skb) 433e022c2f0SKrzysztof Hałasa { 434e022c2f0SKrzysztof Hałasa struct hdlc_header *hdr = (struct hdlc_header*)skb->data; 435e022c2f0SKrzysztof Hałasa struct net_device *dev = skb->dev; 436e022c2f0SKrzysztof Hałasa struct ppp *ppp = get_ppp(dev); 437e022c2f0SKrzysztof Hałasa struct proto *proto; 438e022c2f0SKrzysztof Hałasa struct cp_header *cp; 439e022c2f0SKrzysztof Hałasa unsigned long flags; 440e022c2f0SKrzysztof Hałasa unsigned int len; 441e022c2f0SKrzysztof Hałasa u16 pid; 442e022c2f0SKrzysztof Hałasa #if DEBUG_CP 443e022c2f0SKrzysztof Hałasa int i; 444e022c2f0SKrzysztof Hałasa char *ptr; 445e022c2f0SKrzysztof Hałasa #endif 446e022c2f0SKrzysztof Hałasa 447e022c2f0SKrzysztof Hałasa spin_lock_irqsave(&ppp->lock, flags); 448e022c2f0SKrzysztof Hałasa /* Check HDLC header */ 449e022c2f0SKrzysztof Hałasa if (skb->len < sizeof(struct hdlc_header)) 450e022c2f0SKrzysztof Hałasa goto rx_error; 451e022c2f0SKrzysztof Hałasa cp = (struct cp_header*)skb_pull(skb, sizeof(struct hdlc_header)); 452e022c2f0SKrzysztof Hałasa if (hdr->address != HDLC_ADDR_ALLSTATIONS || 453e022c2f0SKrzysztof Hałasa hdr->control != HDLC_CTRL_UI) 454e022c2f0SKrzysztof Hałasa goto rx_error; 455e022c2f0SKrzysztof Hałasa 456e022c2f0SKrzysztof Hałasa pid = ntohs(hdr->protocol); 457e022c2f0SKrzysztof Hałasa proto = get_proto(dev, pid); 458e022c2f0SKrzysztof Hałasa if (!proto) { 459e022c2f0SKrzysztof Hałasa if (ppp->protos[IDX_LCP].state == OPENED) 460e022c2f0SKrzysztof Hałasa ppp_tx_cp(dev, PID_LCP, LCP_PROTO_REJ, 461e022c2f0SKrzysztof Hałasa ++ppp->seq, skb->len + 2, &hdr->protocol); 462e022c2f0SKrzysztof Hałasa goto rx_error; 463e022c2f0SKrzysztof Hałasa } 464e022c2f0SKrzysztof Hałasa 465e022c2f0SKrzysztof Hałasa len = ntohs(cp->len); 466e022c2f0SKrzysztof Hałasa if (len < sizeof(struct cp_header) /* no complete CP header? */ || 467e022c2f0SKrzysztof Hałasa skb->len < len /* truncated packet? */) 468e022c2f0SKrzysztof Hałasa goto rx_error; 469e022c2f0SKrzysztof Hałasa skb_pull(skb, sizeof(struct cp_header)); 470e022c2f0SKrzysztof Hałasa len -= sizeof(struct cp_header); 471e022c2f0SKrzysztof Hałasa 472e022c2f0SKrzysztof Hałasa /* HDLC and CP headers stripped from skb */ 473e022c2f0SKrzysztof Hałasa #if DEBUG_CP 474e022c2f0SKrzysztof Hałasa if (cp->code < CP_CODES) 475e022c2f0SKrzysztof Hałasa sprintf(debug_buffer, "[%s id 0x%X]", code_names[cp->code], 476e022c2f0SKrzysztof Hałasa cp->id); 477e022c2f0SKrzysztof Hałasa else 478e022c2f0SKrzysztof Hałasa sprintf(debug_buffer, "[code %u id 0x%X]", cp->code, cp->id); 479e022c2f0SKrzysztof Hałasa ptr = debug_buffer + strlen(debug_buffer); 480e022c2f0SKrzysztof Hałasa for (i = 0; i < min_t(unsigned int, len, DEBUG_CP); i++) { 481e022c2f0SKrzysztof Hałasa sprintf(ptr, " %02X", skb->data[i]); 482e022c2f0SKrzysztof Hałasa ptr += strlen(ptr); 483e022c2f0SKrzysztof Hałasa } 484e022c2f0SKrzysztof Hałasa printk(KERN_DEBUG "%s: RX %s %s\n", dev->name, proto_name(pid), 485e022c2f0SKrzysztof Hałasa debug_buffer); 486e022c2f0SKrzysztof Hałasa #endif 487e022c2f0SKrzysztof Hałasa 488e022c2f0SKrzysztof Hałasa /* LCP only */ 489e022c2f0SKrzysztof Hałasa if (pid == PID_LCP) 490e022c2f0SKrzysztof Hałasa switch (cp->code) { 491e022c2f0SKrzysztof Hałasa case LCP_PROTO_REJ: 492e022c2f0SKrzysztof Hałasa pid = ntohs(*(__be16*)skb->data); 493e022c2f0SKrzysztof Hałasa if (pid == PID_LCP || pid == PID_IPCP || 494e022c2f0SKrzysztof Hałasa pid == PID_IPV6CP) 495e022c2f0SKrzysztof Hałasa ppp_cp_event(dev, pid, RXJ_BAD, 0, 0, 496e022c2f0SKrzysztof Hałasa 0, NULL); 497e022c2f0SKrzysztof Hałasa goto out; 498e022c2f0SKrzysztof Hałasa 499e022c2f0SKrzysztof Hałasa case LCP_ECHO_REQ: /* send Echo-Reply */ 500e022c2f0SKrzysztof Hałasa if (len >= 4 && proto->state == OPENED) 501e022c2f0SKrzysztof Hałasa ppp_tx_cp(dev, PID_LCP, LCP_ECHO_REPLY, 502e022c2f0SKrzysztof Hałasa cp->id, len - 4, skb->data + 4); 503e022c2f0SKrzysztof Hałasa goto out; 504e022c2f0SKrzysztof Hałasa 505e022c2f0SKrzysztof Hałasa case LCP_ECHO_REPLY: 506e022c2f0SKrzysztof Hałasa if (cp->id == ppp->echo_id) 507e022c2f0SKrzysztof Hałasa ppp->last_pong = jiffies; 508e022c2f0SKrzysztof Hałasa goto out; 509e022c2f0SKrzysztof Hałasa 510e022c2f0SKrzysztof Hałasa case LCP_DISC_REQ: /* discard */ 511e022c2f0SKrzysztof Hałasa goto out; 512e022c2f0SKrzysztof Hałasa } 513e022c2f0SKrzysztof Hałasa 514e022c2f0SKrzysztof Hałasa /* LCP, IPCP and IPV6CP */ 515e022c2f0SKrzysztof Hałasa switch (cp->code) { 516e022c2f0SKrzysztof Hałasa case CP_CONF_REQ: 517e022c2f0SKrzysztof Hałasa ppp_cp_parse_cr(dev, pid, cp->id, len, skb->data); 518*c8d755b5SJesper Juhl break; 519e022c2f0SKrzysztof Hałasa 520e022c2f0SKrzysztof Hałasa case CP_CONF_ACK: 521e022c2f0SKrzysztof Hałasa if (cp->id == proto->cr_id) 522e022c2f0SKrzysztof Hałasa ppp_cp_event(dev, pid, RCA, 0, 0, 0, NULL); 523*c8d755b5SJesper Juhl break; 524e022c2f0SKrzysztof Hałasa 525e022c2f0SKrzysztof Hałasa case CP_CONF_REJ: 526e022c2f0SKrzysztof Hałasa case CP_CONF_NAK: 527e022c2f0SKrzysztof Hałasa if (cp->id == proto->cr_id) 528e022c2f0SKrzysztof Hałasa ppp_cp_event(dev, pid, RCN, 0, 0, 0, NULL); 529*c8d755b5SJesper Juhl break; 530e022c2f0SKrzysztof Hałasa 531e022c2f0SKrzysztof Hałasa case CP_TERM_REQ: 532e022c2f0SKrzysztof Hałasa ppp_cp_event(dev, pid, RTR, 0, cp->id, 0, NULL); 533*c8d755b5SJesper Juhl break; 534e022c2f0SKrzysztof Hałasa 535e022c2f0SKrzysztof Hałasa case CP_TERM_ACK: 536e022c2f0SKrzysztof Hałasa ppp_cp_event(dev, pid, RTA, 0, 0, 0, NULL); 537*c8d755b5SJesper Juhl break; 538e022c2f0SKrzysztof Hałasa 539e022c2f0SKrzysztof Hałasa case CP_CODE_REJ: 540e022c2f0SKrzysztof Hałasa ppp_cp_event(dev, pid, RXJ_BAD, 0, 0, 0, NULL); 541*c8d755b5SJesper Juhl break; 542e022c2f0SKrzysztof Hałasa 543e022c2f0SKrzysztof Hałasa default: 544e022c2f0SKrzysztof Hałasa len += sizeof(struct cp_header); 545e022c2f0SKrzysztof Hałasa if (len > dev->mtu) 546e022c2f0SKrzysztof Hałasa len = dev->mtu; 547e022c2f0SKrzysztof Hałasa ppp_cp_event(dev, pid, RUC, 0, 0, len, cp); 548*c8d755b5SJesper Juhl break; 549e022c2f0SKrzysztof Hałasa } 550e022c2f0SKrzysztof Hałasa goto out; 551e022c2f0SKrzysztof Hałasa 552e022c2f0SKrzysztof Hałasa rx_error: 553e022c2f0SKrzysztof Hałasa dev->stats.rx_errors++; 554e022c2f0SKrzysztof Hałasa out: 555e022c2f0SKrzysztof Hałasa spin_unlock_irqrestore(&ppp->lock, flags); 556e022c2f0SKrzysztof Hałasa dev_kfree_skb_any(skb); 557e022c2f0SKrzysztof Hałasa ppp_tx_flush(); 558e022c2f0SKrzysztof Hałasa return NET_RX_DROP; 559e022c2f0SKrzysztof Hałasa } 560e022c2f0SKrzysztof Hałasa 561e022c2f0SKrzysztof Hałasa static void ppp_timer(unsigned long arg) 562e022c2f0SKrzysztof Hałasa { 563e022c2f0SKrzysztof Hałasa struct proto *proto = (struct proto *)arg; 564e022c2f0SKrzysztof Hałasa struct ppp *ppp = get_ppp(proto->dev); 565e022c2f0SKrzysztof Hałasa unsigned long flags; 566e022c2f0SKrzysztof Hałasa 567e022c2f0SKrzysztof Hałasa spin_lock_irqsave(&ppp->lock, flags); 568e022c2f0SKrzysztof Hałasa switch (proto->state) { 569e022c2f0SKrzysztof Hałasa case STOPPING: 570e022c2f0SKrzysztof Hałasa case REQ_SENT: 571e022c2f0SKrzysztof Hałasa case ACK_RECV: 572e022c2f0SKrzysztof Hałasa case ACK_SENT: 573e022c2f0SKrzysztof Hałasa if (proto->restart_counter) { 574e022c2f0SKrzysztof Hałasa ppp_cp_event(proto->dev, proto->pid, TO_GOOD, 0, 0, 575e022c2f0SKrzysztof Hałasa 0, NULL); 576e022c2f0SKrzysztof Hałasa proto->restart_counter--; 577e022c2f0SKrzysztof Hałasa } else 578e022c2f0SKrzysztof Hałasa ppp_cp_event(proto->dev, proto->pid, TO_BAD, 0, 0, 579e022c2f0SKrzysztof Hałasa 0, NULL); 580e022c2f0SKrzysztof Hałasa break; 581e022c2f0SKrzysztof Hałasa 582e022c2f0SKrzysztof Hałasa case OPENED: 583e022c2f0SKrzysztof Hałasa if (proto->pid != PID_LCP) 584e022c2f0SKrzysztof Hałasa break; 585e022c2f0SKrzysztof Hałasa if (time_after(jiffies, ppp->last_pong + 586e022c2f0SKrzysztof Hałasa ppp->keepalive_timeout * HZ)) { 58712a3bfefSJoe Perches netdev_info(proto->dev, "Link down\n"); 588e022c2f0SKrzysztof Hałasa ppp_cp_event(proto->dev, PID_LCP, STOP, 0, 0, 0, NULL); 589e022c2f0SKrzysztof Hałasa ppp_cp_event(proto->dev, PID_LCP, START, 0, 0, 0, NULL); 590e022c2f0SKrzysztof Hałasa } else { /* send keep-alive packet */ 591e022c2f0SKrzysztof Hałasa ppp->echo_id = ++ppp->seq; 592e022c2f0SKrzysztof Hałasa ppp_tx_cp(proto->dev, PID_LCP, LCP_ECHO_REQ, 593e022c2f0SKrzysztof Hałasa ppp->echo_id, 0, NULL); 594e022c2f0SKrzysztof Hałasa proto->timer.expires = jiffies + 595e022c2f0SKrzysztof Hałasa ppp->keepalive_interval * HZ; 596e022c2f0SKrzysztof Hałasa add_timer(&proto->timer); 597e022c2f0SKrzysztof Hałasa } 598e022c2f0SKrzysztof Hałasa break; 599e022c2f0SKrzysztof Hałasa } 600e022c2f0SKrzysztof Hałasa spin_unlock_irqrestore(&ppp->lock, flags); 601e022c2f0SKrzysztof Hałasa ppp_tx_flush(); 602e022c2f0SKrzysztof Hałasa } 603e022c2f0SKrzysztof Hałasa 604e022c2f0SKrzysztof Hałasa 605e022c2f0SKrzysztof Hałasa static void ppp_start(struct net_device *dev) 606e022c2f0SKrzysztof Hałasa { 607e022c2f0SKrzysztof Hałasa struct ppp *ppp = get_ppp(dev); 608e022c2f0SKrzysztof Hałasa int i; 609e022c2f0SKrzysztof Hałasa 610e022c2f0SKrzysztof Hałasa for (i = 0; i < IDX_COUNT; i++) { 611e022c2f0SKrzysztof Hałasa struct proto *proto = &ppp->protos[i]; 612e022c2f0SKrzysztof Hałasa proto->dev = dev; 613e022c2f0SKrzysztof Hałasa init_timer(&proto->timer); 614e022c2f0SKrzysztof Hałasa proto->timer.function = ppp_timer; 615e022c2f0SKrzysztof Hałasa proto->timer.data = (unsigned long)proto; 616e022c2f0SKrzysztof Hałasa proto->state = CLOSED; 617e022c2f0SKrzysztof Hałasa } 618e022c2f0SKrzysztof Hałasa ppp->protos[IDX_LCP].pid = PID_LCP; 619e022c2f0SKrzysztof Hałasa ppp->protos[IDX_IPCP].pid = PID_IPCP; 620e022c2f0SKrzysztof Hałasa ppp->protos[IDX_IPV6CP].pid = PID_IPV6CP; 621e022c2f0SKrzysztof Hałasa 622e022c2f0SKrzysztof Hałasa ppp_cp_event(dev, PID_LCP, START, 0, 0, 0, NULL); 623e022c2f0SKrzysztof Hałasa } 624e022c2f0SKrzysztof Hałasa 625e022c2f0SKrzysztof Hałasa static void ppp_stop(struct net_device *dev) 626e022c2f0SKrzysztof Hałasa { 627e022c2f0SKrzysztof Hałasa ppp_cp_event(dev, PID_LCP, STOP, 0, 0, 0, NULL); 628e022c2f0SKrzysztof Hałasa } 6291da177e4SLinus Torvalds 63031f634a6SKrzysztof Halasa static void ppp_close(struct net_device *dev) 63131f634a6SKrzysztof Halasa { 63231f634a6SKrzysztof Halasa ppp_tx_flush(); 63331f634a6SKrzysztof Halasa } 63431f634a6SKrzysztof Halasa 635eb2a2fd9SKrzysztof Halasa static struct hdlc_proto proto = { 636e022c2f0SKrzysztof Hałasa .start = ppp_start, 637e022c2f0SKrzysztof Hałasa .stop = ppp_stop, 63831f634a6SKrzysztof Halasa .close = ppp_close, 639eb2a2fd9SKrzysztof Halasa .type_trans = ppp_type_trans, 640eb2a2fd9SKrzysztof Halasa .ioctl = ppp_ioctl, 641e022c2f0SKrzysztof Hałasa .netif_rx = ppp_rx, 642eb2a2fd9SKrzysztof Halasa .module = THIS_MODULE, 643eb2a2fd9SKrzysztof Halasa }; 644eb2a2fd9SKrzysztof Halasa 645e022c2f0SKrzysztof Hałasa static const struct header_ops ppp_header_ops = { 646e022c2f0SKrzysztof Hałasa .create = ppp_hard_header, 647e022c2f0SKrzysztof Hałasa }; 648eb2a2fd9SKrzysztof Halasa 649eb2a2fd9SKrzysztof Halasa static int ppp_ioctl(struct net_device *dev, struct ifreq *ifr) 6501da177e4SLinus Torvalds { 6511da177e4SLinus Torvalds hdlc_device *hdlc = dev_to_hdlc(dev); 652e022c2f0SKrzysztof Hałasa struct ppp *ppp; 6531da177e4SLinus Torvalds int result; 6541da177e4SLinus Torvalds 6551da177e4SLinus Torvalds switch (ifr->ifr_settings.type) { 6561da177e4SLinus Torvalds case IF_GET_PROTO: 657eb2a2fd9SKrzysztof Halasa if (dev_to_hdlc(dev)->proto != &proto) 658eb2a2fd9SKrzysztof Halasa return -EINVAL; 6591da177e4SLinus Torvalds ifr->ifr_settings.type = IF_PROTO_PPP; 6601da177e4SLinus Torvalds return 0; /* return protocol only, no settable parameters */ 6611da177e4SLinus Torvalds 6621da177e4SLinus Torvalds case IF_PROTO_PPP: 6631da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 6641da177e4SLinus Torvalds return -EPERM; 6651da177e4SLinus Torvalds 6661da177e4SLinus Torvalds if (dev->flags & IFF_UP) 6671da177e4SLinus Torvalds return -EBUSY; 6681da177e4SLinus Torvalds 6691da177e4SLinus Torvalds /* no settable parameters */ 6701da177e4SLinus Torvalds 6711da177e4SLinus Torvalds result = hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT); 6721da177e4SLinus Torvalds if (result) 6731da177e4SLinus Torvalds return result; 6741da177e4SLinus Torvalds 675e022c2f0SKrzysztof Hałasa result = attach_hdlc_protocol(dev, &proto, sizeof(struct ppp)); 676eb2a2fd9SKrzysztof Halasa if (result) 677eb2a2fd9SKrzysztof Halasa return result; 678e022c2f0SKrzysztof Hałasa 679e022c2f0SKrzysztof Hałasa ppp = get_ppp(dev); 680e022c2f0SKrzysztof Hałasa spin_lock_init(&ppp->lock); 681e022c2f0SKrzysztof Hałasa ppp->req_timeout = 2; 682e022c2f0SKrzysztof Hałasa ppp->cr_retries = 10; 683e022c2f0SKrzysztof Hałasa ppp->term_retries = 2; 684e022c2f0SKrzysztof Hałasa ppp->keepalive_interval = 10; 685e022c2f0SKrzysztof Hałasa ppp->keepalive_timeout = 60; 686e022c2f0SKrzysztof Hałasa 687e022c2f0SKrzysztof Hałasa dev->hard_header_len = sizeof(struct hdlc_header); 688e022c2f0SKrzysztof Hałasa dev->header_ops = &ppp_header_ops; 6891da177e4SLinus Torvalds dev->type = ARPHRD_PPP; 690e022c2f0SKrzysztof Hałasa netif_dormant_on(dev); 6911da177e4SLinus Torvalds return 0; 6921da177e4SLinus Torvalds } 6931da177e4SLinus Torvalds 6941da177e4SLinus Torvalds return -EINVAL; 6951da177e4SLinus Torvalds } 696eb2a2fd9SKrzysztof Halasa 697eb2a2fd9SKrzysztof Halasa 698eb2a2fd9SKrzysztof Halasa static int __init mod_init(void) 699eb2a2fd9SKrzysztof Halasa { 700e022c2f0SKrzysztof Hałasa skb_queue_head_init(&tx_queue); 701eb2a2fd9SKrzysztof Halasa register_hdlc_protocol(&proto); 702eb2a2fd9SKrzysztof Halasa return 0; 703eb2a2fd9SKrzysztof Halasa } 704eb2a2fd9SKrzysztof Halasa 705eb2a2fd9SKrzysztof Halasa static void __exit mod_exit(void) 706eb2a2fd9SKrzysztof Halasa { 707eb2a2fd9SKrzysztof Halasa unregister_hdlc_protocol(&proto); 708eb2a2fd9SKrzysztof Halasa } 709eb2a2fd9SKrzysztof Halasa 710eb2a2fd9SKrzysztof Halasa 711eb2a2fd9SKrzysztof Halasa module_init(mod_init); 712eb2a2fd9SKrzysztof Halasa module_exit(mod_exit); 713eb2a2fd9SKrzysztof Halasa 714eb2a2fd9SKrzysztof Halasa MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>"); 715eb2a2fd9SKrzysztof Halasa MODULE_DESCRIPTION("PPP protocol support for generic HDLC"); 716eb2a2fd9SKrzysztof Halasa MODULE_LICENSE("GPL v2"); 717