hdlc_ppp.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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. netdev_warn(dev, "out of memory in ppp_tx_cp()\n");
  193. return;
  194. }
  195. skb_reserve(skb, sizeof(struct hdlc_header));
  196. cp = (struct cp_header *)skb_put(skb, sizeof(struct cp_header));
  197. cp->code = code;
  198. cp->id = id;
  199. cp->len = htons(sizeof(struct cp_header) + magic_len + len);
  200. if (magic_len)
  201. memcpy(skb_put(skb, magic_len), &magic, magic_len);
  202. if (len)
  203. memcpy(skb_put(skb, len), data, len);
  204. #if DEBUG_CP
  205. BUG_ON(code >= CP_CODES);
  206. ptr = debug_buffer;
  207. *ptr = '\x0';
  208. for (i = 0; i < min_t(unsigned int, magic_len + len, DEBUG_CP); i++) {
  209. sprintf(ptr, " %02X", skb->data[sizeof(struct cp_header) + i]);
  210. ptr += strlen(ptr);
  211. }
  212. printk(KERN_DEBUG "%s: TX %s [%s id 0x%X]%s\n", dev->name,
  213. proto_name(pid), code_names[code], id, debug_buffer);
  214. #endif
  215. ppp_hard_header(skb, dev, pid, NULL, NULL, 0);
  216. skb->priority = TC_PRIO_CONTROL;
  217. skb->dev = dev;
  218. skb_reset_network_header(skb);
  219. skb_queue_tail(&tx_queue, skb);
  220. }
  221. /* State transition table (compare STD-51)
  222. Events Actions
  223. TO+ = Timeout with counter > 0 irc = Initialize-Restart-Count
  224. TO- = Timeout with counter expired zrc = Zero-Restart-Count
  225. RCR+ = Receive-Configure-Request (Good) scr = Send-Configure-Request
  226. RCR- = Receive-Configure-Request (Bad)
  227. RCA = Receive-Configure-Ack sca = Send-Configure-Ack
  228. RCN = Receive-Configure-Nak/Rej scn = Send-Configure-Nak/Rej
  229. RTR = Receive-Terminate-Request str = Send-Terminate-Request
  230. RTA = Receive-Terminate-Ack sta = Send-Terminate-Ack
  231. RUC = Receive-Unknown-Code scj = Send-Code-Reject
  232. RXJ+ = Receive-Code-Reject (permitted)
  233. or Receive-Protocol-Reject
  234. RXJ- = Receive-Code-Reject (catastrophic)
  235. or Receive-Protocol-Reject
  236. */
  237. static int cp_table[EVENTS][STATES] = {
  238. /* CLOSED STOPPED STOPPING REQ_SENT ACK_RECV ACK_SENT OPENED
  239. 0 1 2 3 4 5 6 */
  240. {IRC|SCR|3, INV , INV , INV , INV , INV , INV }, /* START */
  241. { INV , 0 , 0 , 0 , 0 , 0 , 0 }, /* STOP */
  242. { INV , INV ,STR|2, SCR|3 ,SCR|3, SCR|5 , INV }, /* TO+ */
  243. { INV , INV , 1 , 1 , 1 , 1 , INV }, /* TO- */
  244. { STA|0 ,IRC|SCR|SCA|5, 2 , SCA|5 ,SCA|6, SCA|5 ,SCR|SCA|5}, /* RCR+ */
  245. { STA|0 ,IRC|SCR|SCN|3, 2 , SCN|3 ,SCN|4, SCN|3 ,SCR|SCN|3}, /* RCR- */
  246. { STA|0 , STA|1 , 2 , IRC|4 ,SCR|3, 6 , SCR|3 }, /* RCA */
  247. { STA|0 , STA|1 , 2 ,IRC|SCR|3,SCR|3,IRC|SCR|5, SCR|3 }, /* RCN */
  248. { STA|0 , STA|1 ,STA|2, STA|3 ,STA|3, STA|3 ,ZRC|STA|2}, /* RTR */
  249. { 0 , 1 , 1 , 3 , 3 , 5 , SCR|3 }, /* RTA */
  250. { SCJ|0 , SCJ|1 ,SCJ|2, SCJ|3 ,SCJ|4, SCJ|5 , SCJ|6 }, /* RUC */
  251. { 0 , 1 , 2 , 3 , 3 , 5 , 6 }, /* RXJ+ */
  252. { 0 , 1 , 1 , 1 , 1 , 1 ,IRC|STR|2}, /* RXJ- */
  253. };
  254. /* SCA: RCR+ must supply id, len and data
  255. SCN: RCR- must supply code, id, len and data
  256. STA: RTR must supply id
  257. SCJ: RUC must supply CP packet len and data */
  258. static void ppp_cp_event(struct net_device *dev, u16 pid, u16 event, u8 code,
  259. u8 id, unsigned int len, const void *data)
  260. {
  261. int old_state, action;
  262. struct ppp *ppp = get_ppp(dev);
  263. struct proto *proto = get_proto(dev, pid);
  264. old_state = proto->state;
  265. BUG_ON(old_state >= STATES);
  266. BUG_ON(event >= EVENTS);
  267. #if DEBUG_STATE
  268. printk(KERN_DEBUG "%s: %s ppp_cp_event(%s) %s ...\n", dev->name,
  269. proto_name(pid), event_names[event], state_names[proto->state]);
  270. #endif
  271. action = cp_table[event][old_state];
  272. proto->state = action & STATE_MASK;
  273. if (action & (SCR | STR)) /* set Configure-Req/Terminate-Req timer */
  274. mod_timer(&proto->timer, proto->timeout =
  275. jiffies + ppp->req_timeout * HZ);
  276. if (action & ZRC)
  277. proto->restart_counter = 0;
  278. if (action & IRC)
  279. proto->restart_counter = (proto->state == STOPPING) ?
  280. ppp->term_retries : ppp->cr_retries;
  281. if (action & SCR) /* send Configure-Request */
  282. ppp_tx_cp(dev, pid, CP_CONF_REQ, proto->cr_id = ++ppp->seq,
  283. 0, NULL);
  284. if (action & SCA) /* send Configure-Ack */
  285. ppp_tx_cp(dev, pid, CP_CONF_ACK, id, len, data);
  286. if (action & SCN) /* send Configure-Nak/Reject */
  287. ppp_tx_cp(dev, pid, code, id, len, data);
  288. if (action & STR) /* send Terminate-Request */
  289. ppp_tx_cp(dev, pid, CP_TERM_REQ, ++ppp->seq, 0, NULL);
  290. if (action & STA) /* send Terminate-Ack */
  291. ppp_tx_cp(dev, pid, CP_TERM_ACK, id, 0, NULL);
  292. if (action & SCJ) /* send Code-Reject */
  293. ppp_tx_cp(dev, pid, CP_CODE_REJ, ++ppp->seq, len, data);
  294. if (old_state != OPENED && proto->state == OPENED) {
  295. netdev_info(dev, "%s up\n", proto_name(pid));
  296. if (pid == PID_LCP) {
  297. netif_dormant_off(dev);
  298. ppp_cp_event(dev, PID_IPCP, START, 0, 0, 0, NULL);
  299. ppp_cp_event(dev, PID_IPV6CP, START, 0, 0, 0, NULL);
  300. ppp->last_pong = jiffies;
  301. mod_timer(&proto->timer, proto->timeout =
  302. jiffies + ppp->keepalive_interval * HZ);
  303. }
  304. }
  305. if (old_state == OPENED && proto->state != OPENED) {
  306. netdev_info(dev, "%s down\n", proto_name(pid));
  307. if (pid == PID_LCP) {
  308. netif_dormant_on(dev);
  309. ppp_cp_event(dev, PID_IPCP, STOP, 0, 0, 0, NULL);
  310. ppp_cp_event(dev, PID_IPV6CP, STOP, 0, 0, 0, NULL);
  311. }
  312. }
  313. if (old_state != CLOSED && proto->state == CLOSED)
  314. del_timer(&proto->timer);
  315. #if DEBUG_STATE
  316. printk(KERN_DEBUG "%s: %s ppp_cp_event(%s) ... %s\n", dev->name,
  317. proto_name(pid), event_names[event], state_names[proto->state]);
  318. #endif
  319. }
  320. static void ppp_cp_parse_cr(struct net_device *dev, u16 pid, u8 id,
  321. unsigned int req_len, const u8 *data)
  322. {
  323. static u8 const valid_accm[6] = { LCP_OPTION_ACCM, 6, 0, 0, 0, 0 };
  324. const u8 *opt;
  325. u8 *out;
  326. unsigned int len = req_len, nak_len = 0, rej_len = 0;
  327. if (!(out = kmalloc(len, GFP_ATOMIC))) {
  328. dev->stats.rx_dropped++;
  329. return; /* out of memory, ignore CR packet */
  330. }
  331. for (opt = data; len; len -= opt[1], opt += opt[1]) {
  332. if (len < 2 || opt[1] < 2 || len < opt[1])
  333. goto err_out;
  334. if (pid == PID_LCP)
  335. switch (opt[0]) {
  336. case LCP_OPTION_MRU:
  337. continue; /* MRU always OK and > 1500 bytes? */
  338. case LCP_OPTION_ACCM: /* async control character map */
  339. if (opt[1] < sizeof(valid_accm))
  340. goto err_out;
  341. if (!memcmp(opt, valid_accm,
  342. sizeof(valid_accm)))
  343. continue;
  344. if (!rej_len) { /* NAK it */
  345. memcpy(out + nak_len, valid_accm,
  346. sizeof(valid_accm));
  347. nak_len += sizeof(valid_accm);
  348. continue;
  349. }
  350. break;
  351. case LCP_OPTION_MAGIC:
  352. if (len < 6)
  353. goto err_out;
  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. return;
  371. err_out:
  372. dev->stats.rx_errors++;
  373. kfree(out);
  374. }
  375. static int ppp_rx(struct sk_buff *skb)
  376. {
  377. struct hdlc_header *hdr = (struct hdlc_header*)skb->data;
  378. struct net_device *dev = skb->dev;
  379. struct ppp *ppp = get_ppp(dev);
  380. struct proto *proto;
  381. struct cp_header *cp;
  382. unsigned long flags;
  383. unsigned int len;
  384. u16 pid;
  385. #if DEBUG_CP
  386. int i;
  387. char *ptr;
  388. #endif
  389. spin_lock_irqsave(&ppp->lock, flags);
  390. /* Check HDLC header */
  391. if (skb->len < sizeof(struct hdlc_header))
  392. goto rx_error;
  393. cp = (struct cp_header*)skb_pull(skb, sizeof(struct hdlc_header));
  394. if (hdr->address != HDLC_ADDR_ALLSTATIONS ||
  395. hdr->control != HDLC_CTRL_UI)
  396. goto rx_error;
  397. pid = ntohs(hdr->protocol);
  398. proto = get_proto(dev, pid);
  399. if (!proto) {
  400. if (ppp->protos[IDX_LCP].state == OPENED)
  401. ppp_tx_cp(dev, PID_LCP, LCP_PROTO_REJ,
  402. ++ppp->seq, skb->len + 2, &hdr->protocol);
  403. goto rx_error;
  404. }
  405. len = ntohs(cp->len);
  406. if (len < sizeof(struct cp_header) /* no complete CP header? */ ||
  407. skb->len < len /* truncated packet? */)
  408. goto rx_error;
  409. skb_pull(skb, sizeof(struct cp_header));
  410. len -= sizeof(struct cp_header);
  411. /* HDLC and CP headers stripped from skb */
  412. #if DEBUG_CP
  413. if (cp->code < CP_CODES)
  414. sprintf(debug_buffer, "[%s id 0x%X]", code_names[cp->code],
  415. cp->id);
  416. else
  417. sprintf(debug_buffer, "[code %u id 0x%X]", cp->code, cp->id);
  418. ptr = debug_buffer + strlen(debug_buffer);
  419. for (i = 0; i < min_t(unsigned int, len, DEBUG_CP); i++) {
  420. sprintf(ptr, " %02X", skb->data[i]);
  421. ptr += strlen(ptr);
  422. }
  423. printk(KERN_DEBUG "%s: RX %s %s\n", dev->name, proto_name(pid),
  424. debug_buffer);
  425. #endif
  426. /* LCP only */
  427. if (pid == PID_LCP)
  428. switch (cp->code) {
  429. case LCP_PROTO_REJ:
  430. pid = ntohs(*(__be16*)skb->data);
  431. if (pid == PID_LCP || pid == PID_IPCP ||
  432. pid == PID_IPV6CP)
  433. ppp_cp_event(dev, pid, RXJ_BAD, 0, 0,
  434. 0, NULL);
  435. goto out;
  436. case LCP_ECHO_REQ: /* send Echo-Reply */
  437. if (len >= 4 && proto->state == OPENED)
  438. ppp_tx_cp(dev, PID_LCP, LCP_ECHO_REPLY,
  439. cp->id, len - 4, skb->data + 4);
  440. goto out;
  441. case LCP_ECHO_REPLY:
  442. if (cp->id == ppp->echo_id)
  443. ppp->last_pong = jiffies;
  444. goto out;
  445. case LCP_DISC_REQ: /* discard */
  446. goto out;
  447. }
  448. /* LCP, IPCP and IPV6CP */
  449. switch (cp->code) {
  450. case CP_CONF_REQ:
  451. ppp_cp_parse_cr(dev, pid, cp->id, len, skb->data);
  452. break;
  453. case CP_CONF_ACK:
  454. if (cp->id == proto->cr_id)
  455. ppp_cp_event(dev, pid, RCA, 0, 0, 0, NULL);
  456. break;
  457. case CP_CONF_REJ:
  458. case CP_CONF_NAK:
  459. if (cp->id == proto->cr_id)
  460. ppp_cp_event(dev, pid, RCN, 0, 0, 0, NULL);
  461. break;
  462. case CP_TERM_REQ:
  463. ppp_cp_event(dev, pid, RTR, 0, cp->id, 0, NULL);
  464. break;
  465. case CP_TERM_ACK:
  466. ppp_cp_event(dev, pid, RTA, 0, 0, 0, NULL);
  467. break;
  468. case CP_CODE_REJ:
  469. ppp_cp_event(dev, pid, RXJ_BAD, 0, 0, 0, NULL);
  470. break;
  471. default:
  472. len += sizeof(struct cp_header);
  473. if (len > dev->mtu)
  474. len = dev->mtu;
  475. ppp_cp_event(dev, pid, RUC, 0, 0, len, cp);
  476. break;
  477. }
  478. goto out;
  479. rx_error:
  480. dev->stats.rx_errors++;
  481. out:
  482. spin_unlock_irqrestore(&ppp->lock, flags);
  483. dev_kfree_skb_any(skb);
  484. ppp_tx_flush();
  485. return NET_RX_DROP;
  486. }
  487. static void ppp_timer(unsigned long arg)
  488. {
  489. struct proto *proto = (struct proto *)arg;
  490. struct ppp *ppp = get_ppp(proto->dev);
  491. unsigned long flags;
  492. spin_lock_irqsave(&ppp->lock, flags);
  493. switch (proto->state) {
  494. case STOPPING:
  495. case REQ_SENT:
  496. case ACK_RECV:
  497. case ACK_SENT:
  498. if (proto->restart_counter) {
  499. ppp_cp_event(proto->dev, proto->pid, TO_GOOD, 0, 0,
  500. 0, NULL);
  501. proto->restart_counter--;
  502. } else
  503. ppp_cp_event(proto->dev, proto->pid, TO_BAD, 0, 0,
  504. 0, NULL);
  505. break;
  506. case OPENED:
  507. if (proto->pid != PID_LCP)
  508. break;
  509. if (time_after(jiffies, ppp->last_pong +
  510. ppp->keepalive_timeout * HZ)) {
  511. netdev_info(proto->dev, "Link down\n");
  512. ppp_cp_event(proto->dev, PID_LCP, STOP, 0, 0, 0, NULL);
  513. ppp_cp_event(proto->dev, PID_LCP, START, 0, 0, 0, NULL);
  514. } else { /* send keep-alive packet */
  515. ppp->echo_id = ++ppp->seq;
  516. ppp_tx_cp(proto->dev, PID_LCP, LCP_ECHO_REQ,
  517. ppp->echo_id, 0, NULL);
  518. proto->timer.expires = jiffies +
  519. ppp->keepalive_interval * HZ;
  520. add_timer(&proto->timer);
  521. }
  522. break;
  523. }
  524. spin_unlock_irqrestore(&ppp->lock, flags);
  525. ppp_tx_flush();
  526. }
  527. static void ppp_start(struct net_device *dev)
  528. {
  529. struct ppp *ppp = get_ppp(dev);
  530. int i;
  531. for (i = 0; i < IDX_COUNT; i++) {
  532. struct proto *proto = &ppp->protos[i];
  533. proto->dev = dev;
  534. init_timer(&proto->timer);
  535. proto->timer.function = ppp_timer;
  536. proto->timer.data = (unsigned long)proto;
  537. proto->state = CLOSED;
  538. }
  539. ppp->protos[IDX_LCP].pid = PID_LCP;
  540. ppp->protos[IDX_IPCP].pid = PID_IPCP;
  541. ppp->protos[IDX_IPV6CP].pid = PID_IPV6CP;
  542. ppp_cp_event(dev, PID_LCP, START, 0, 0, 0, NULL);
  543. }
  544. static void ppp_stop(struct net_device *dev)
  545. {
  546. ppp_cp_event(dev, PID_LCP, STOP, 0, 0, 0, NULL);
  547. }
  548. static void ppp_close(struct net_device *dev)
  549. {
  550. ppp_tx_flush();
  551. }
  552. static struct hdlc_proto proto = {
  553. .start = ppp_start,
  554. .stop = ppp_stop,
  555. .close = ppp_close,
  556. .type_trans = ppp_type_trans,
  557. .ioctl = ppp_ioctl,
  558. .netif_rx = ppp_rx,
  559. .module = THIS_MODULE,
  560. };
  561. static const struct header_ops ppp_header_ops = {
  562. .create = ppp_hard_header,
  563. };
  564. static int ppp_ioctl(struct net_device *dev, struct ifreq *ifr)
  565. {
  566. hdlc_device *hdlc = dev_to_hdlc(dev);
  567. struct ppp *ppp;
  568. int result;
  569. switch (ifr->ifr_settings.type) {
  570. case IF_GET_PROTO:
  571. if (dev_to_hdlc(dev)->proto != &proto)
  572. return -EINVAL;
  573. ifr->ifr_settings.type = IF_PROTO_PPP;
  574. return 0; /* return protocol only, no settable parameters */
  575. case IF_PROTO_PPP:
  576. if (!capable(CAP_NET_ADMIN))
  577. return -EPERM;
  578. if (dev->flags & IFF_UP)
  579. return -EBUSY;
  580. /* no settable parameters */
  581. result = hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
  582. if (result)
  583. return result;
  584. result = attach_hdlc_protocol(dev, &proto, sizeof(struct ppp));
  585. if (result)
  586. return result;
  587. ppp = get_ppp(dev);
  588. spin_lock_init(&ppp->lock);
  589. ppp->req_timeout = 2;
  590. ppp->cr_retries = 10;
  591. ppp->term_retries = 2;
  592. ppp->keepalive_interval = 10;
  593. ppp->keepalive_timeout = 60;
  594. dev->hard_header_len = sizeof(struct hdlc_header);
  595. dev->header_ops = &ppp_header_ops;
  596. dev->type = ARPHRD_PPP;
  597. netif_dormant_on(dev);
  598. return 0;
  599. }
  600. return -EINVAL;
  601. }
  602. static int __init mod_init(void)
  603. {
  604. skb_queue_head_init(&tx_queue);
  605. register_hdlc_protocol(&proto);
  606. return 0;
  607. }
  608. static void __exit mod_exit(void)
  609. {
  610. unregister_hdlc_protocol(&proto);
  611. }
  612. module_init(mod_init);
  613. module_exit(mod_exit);
  614. MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
  615. MODULE_DESCRIPTION("PPP protocol support for generic HDLC");
  616. MODULE_LICENSE("GPL v2");