hdlc_ppp.c 19 KB

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