if_pppoe.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  1. /* $OpenBSD: if_pppoe.c,v 1.46 2015/06/16 11:09:39 mpi Exp $ */
  2. /* $NetBSD: if_pppoe.c,v 1.51 2003/11/28 08:56:48 keihan Exp $ */
  3. /*
  4. * Copyright (c) 2002 The NetBSD Foundation, Inc.
  5. * All rights reserved.
  6. *
  7. * This code is derived from software contributed to The NetBSD Foundation
  8. * by Martin Husemann <martin@NetBSD.org>.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  21. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  23. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include "pppoe.h"
  32. #include "bpfilter.h"
  33. #include <sys/param.h>
  34. #include <sys/systm.h>
  35. #include <sys/kernel.h>
  36. #include <sys/types.h>
  37. #include <sys/timeout.h>
  38. #include <sys/malloc.h>
  39. #include <sys/mbuf.h>
  40. #include <sys/socket.h>
  41. #include <sys/syslog.h>
  42. #include <sys/ioctl.h>
  43. #include <net/if.h>
  44. #include <net/if_var.h>
  45. #include <net/if_types.h>
  46. #include <net/if_sppp.h>
  47. #include <net/if_pppoe.h>
  48. #include <net/netisr.h>
  49. #include <netinet/in.h>
  50. #include <netinet/if_ether.h>
  51. #if NBPFILTER > 0
  52. #include <net/bpf.h>
  53. #endif
  54. #undef PPPOE_DEBUG /* XXX - remove this or make it an option */
  55. #define PPPOEDEBUG(a) ((sc->sc_sppp.pp_if.if_flags & IFF_DEBUG) ? printf a : 0)
  56. struct pppoehdr {
  57. u_int8_t vertype;
  58. u_int8_t code;
  59. u_int16_t session;
  60. u_int16_t plen;
  61. } __packed;
  62. struct pppoetag {
  63. u_int16_t tag;
  64. u_int16_t len;
  65. } __packed;
  66. #define PPPOE_HEADERLEN sizeof(struct pppoehdr)
  67. #define PPPOE_OVERHEAD (PPPOE_HEADERLEN + 2)
  68. #define PPPOE_VERTYPE 0x11 /* VER=1, TYPE = 1 */
  69. #define PPPOE_TAG_EOL 0x0000 /* end of list */
  70. #define PPPOE_TAG_SNAME 0x0101 /* service name */
  71. #define PPPOE_TAG_ACNAME 0x0102 /* access concentrator name */
  72. #define PPPOE_TAG_HUNIQUE 0x0103 /* host unique */
  73. #define PPPOE_TAG_ACCOOKIE 0x0104 /* AC cookie */
  74. #define PPPOE_TAG_VENDOR 0x0105 /* vendor specific */
  75. #define PPPOE_TAG_RELAYSID 0x0110 /* relay session id */
  76. #define PPPOE_TAG_MAX_PAYLOAD 0x0120 /* RFC 4638 max payload */
  77. #define PPPOE_TAG_SNAME_ERR 0x0201 /* service name error */
  78. #define PPPOE_TAG_ACSYS_ERR 0x0202 /* AC system error */
  79. #define PPPOE_TAG_GENERIC_ERR 0x0203 /* generic error */
  80. #define PPPOE_CODE_PADI 0x09 /* Active Discovery Initiation */
  81. #define PPPOE_CODE_PADO 0x07 /* Active Discovery Offer */
  82. #define PPPOE_CODE_PADR 0x19 /* Active Discovery Request */
  83. #define PPPOE_CODE_PADS 0x65 /* Active Discovery Session confirmation */
  84. #define PPPOE_CODE_PADT 0xA7 /* Active Discovery Terminate */
  85. /* two byte PPP protocol discriminator, then IP data */
  86. #define PPPOE_MTU (ETHERMTU - PPPOE_OVERHEAD)
  87. #define PPPOE_MAXMTU PP_MAX_MRU
  88. /* Add a 16 bit unsigned value to a buffer pointed to by PTR */
  89. #define PPPOE_ADD_16(PTR, VAL) \
  90. *(PTR)++ = (VAL) / 256; \
  91. *(PTR)++ = (VAL) % 256
  92. /* Add a complete PPPoE header to the buffer pointed to by PTR */
  93. #define PPPOE_ADD_HEADER(PTR, CODE, SESS, LEN) \
  94. *(PTR)++ = PPPOE_VERTYPE; \
  95. *(PTR)++ = (CODE); \
  96. PPPOE_ADD_16(PTR, SESS); \
  97. PPPOE_ADD_16(PTR, LEN)
  98. #define PPPOE_DISC_TIMEOUT (hz*5) /* base for quick timeout calculation */
  99. #define PPPOE_SLOW_RETRY (hz*60) /* persistent retry interval */
  100. #define PPPOE_DISC_MAXPADI 4 /* retry PADI four times (quickly) */
  101. #define PPPOE_DISC_MAXPADR 2 /* retry PADR twice */
  102. #ifdef PPPOE_SERVER
  103. #define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */
  104. #endif
  105. struct pppoe_softc {
  106. struct sppp sc_sppp; /* contains a struct ifnet as first element */
  107. LIST_ENTRY(pppoe_softc) sc_list;
  108. struct ifnet *sc_eth_if; /* ethernet interface we are using */
  109. int sc_state; /* discovery phase or session connected */
  110. struct ether_addr sc_dest; /* hardware address of concentrator */
  111. u_int16_t sc_session; /* PPPoE session id */
  112. char *sc_service_name; /* if != NULL: requested name of service */
  113. char *sc_concentrator_name; /* if != NULL: requested concentrator id */
  114. u_int8_t *sc_ac_cookie; /* content of AC cookie we must echo back */
  115. size_t sc_ac_cookie_len; /* length of cookie data */
  116. u_int8_t *sc_relay_sid; /* content of relay SID we must echo back */
  117. size_t sc_relay_sid_len; /* length of relay SID data */
  118. #ifdef PPPOE_SERVER
  119. u_int8_t *sc_hunique; /* content of host unique we must echo back */
  120. size_t sc_hunique_len; /* length of host unique */
  121. #endif
  122. u_int32_t sc_unique; /* our unique id */
  123. struct timeout sc_timeout; /* timeout while not in session state */
  124. int sc_padi_retried; /* number of PADI retries already done */
  125. int sc_padr_retried; /* number of PADR retries already done */
  126. struct timeval sc_session_time; /* time the session was established */
  127. };
  128. /* incoming traffic will be queued here */
  129. struct niqueue pppoediscinq = NIQUEUE_INITIALIZER(IFQ_MAXLEN, NETISR_PPPOE);
  130. struct niqueue pppoeinq = NIQUEUE_INITIALIZER(IFQ_MAXLEN, NETISR_PPPOE);
  131. /* input routines */
  132. static void pppoe_disc_input(struct mbuf *);
  133. static void pppoe_dispatch_disc_pkt(struct mbuf *, int);
  134. static void pppoe_data_input(struct mbuf *);
  135. /* management routines */
  136. void pppoeattach(int);
  137. static int pppoe_connect(struct pppoe_softc *);
  138. static int pppoe_disconnect(struct pppoe_softc *);
  139. static void pppoe_abort_connect(struct pppoe_softc *);
  140. static int pppoe_ioctl(struct ifnet *, unsigned long, caddr_t);
  141. static void pppoe_tls(struct sppp *);
  142. static void pppoe_tlf(struct sppp *);
  143. static void pppoe_start(struct ifnet *);
  144. /* internal timeout handling */
  145. static void pppoe_timeout(void *);
  146. /* sending actual protocol control packets */
  147. static int pppoe_send_padi(struct pppoe_softc *);
  148. static int pppoe_send_padr(struct pppoe_softc *);
  149. #ifdef PPPOE_SERVER
  150. static int pppoe_send_pado(struct pppoe_softc *);
  151. static int pppoe_send_pads(struct pppoe_softc *);
  152. #endif
  153. static int pppoe_send_padt(struct ifnet *, u_int, const u_int8_t *);
  154. /* raw output */
  155. static int pppoe_output(struct pppoe_softc *, struct mbuf *);
  156. /* internal helper functions */
  157. static struct pppoe_softc *pppoe_find_softc_by_session(u_int, u_int);
  158. static struct pppoe_softc *pppoe_find_softc_by_hunique(u_int8_t *, size_t, u_int);
  159. static struct mbuf *pppoe_get_mbuf(size_t len);
  160. LIST_HEAD(pppoe_softc_head, pppoe_softc) pppoe_softc_list;
  161. /* interface cloning */
  162. int pppoe_clone_create(struct if_clone *, int);
  163. int pppoe_clone_destroy(struct ifnet *);
  164. struct if_clone pppoe_cloner =
  165. IF_CLONE_INITIALIZER("pppoe", pppoe_clone_create, pppoe_clone_destroy);
  166. /* ARGSUSED */
  167. void
  168. pppoeattach(int count)
  169. {
  170. LIST_INIT(&pppoe_softc_list);
  171. if_clone_attach(&pppoe_cloner);
  172. }
  173. /* Create a new interface. */
  174. int
  175. pppoe_clone_create(struct if_clone *ifc, int unit)
  176. {
  177. struct pppoe_softc *sc, *tmpsc;
  178. u_int32_t unique;
  179. int s;
  180. sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_CANFAIL|M_ZERO);
  181. if (sc == NULL)
  182. return (ENOMEM);
  183. snprintf(sc->sc_sppp.pp_if.if_xname,
  184. sizeof(sc->sc_sppp.pp_if.if_xname),
  185. "pppoe%d", unit);
  186. sc->sc_sppp.pp_if.if_softc = sc;
  187. sc->sc_sppp.pp_if.if_mtu = PPPOE_MTU;
  188. sc->sc_sppp.pp_if.if_flags = IFF_SIMPLEX | IFF_POINTOPOINT | IFF_MULTICAST;
  189. sc->sc_sppp.pp_if.if_type = IFT_PPP;
  190. sc->sc_sppp.pp_if.if_hdrlen = sizeof(struct ether_header) + PPPOE_HEADERLEN;
  191. sc->sc_sppp.pp_flags |= PP_KEEPALIVE | /* use LCP keepalive */
  192. PP_NOFRAMING; /* no serial encapsulation */
  193. sc->sc_sppp.pp_framebytes = PPPOE_HEADERLEN; /* framing added to ppp packets */
  194. sc->sc_sppp.pp_if.if_ioctl = pppoe_ioctl;
  195. sc->sc_sppp.pp_if.if_start = pppoe_start;
  196. sc->sc_sppp.pp_tls = pppoe_tls;
  197. sc->sc_sppp.pp_tlf = pppoe_tlf;
  198. IFQ_SET_MAXLEN(&sc->sc_sppp.pp_if.if_snd, IFQ_MAXLEN);
  199. IFQ_SET_READY(&sc->sc_sppp.pp_if.if_snd);
  200. /* changed to real address later */
  201. memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest));
  202. /* init timer for interface watchdog */
  203. timeout_set(&sc->sc_timeout, pppoe_timeout, sc);
  204. if_attach(&sc->sc_sppp.pp_if);
  205. if_alloc_sadl(&sc->sc_sppp.pp_if);
  206. sppp_attach(&sc->sc_sppp.pp_if);
  207. #if NBPFILTER > 0
  208. bpfattach(&sc->sc_sppp.pp_if.if_bpf, &sc->sc_sppp.pp_if, DLT_PPP_ETHER, 0);
  209. #endif
  210. s = splnet();
  211. retry:
  212. unique = arc4random();
  213. LIST_FOREACH(tmpsc, &pppoe_softc_list, sc_list)
  214. if (tmpsc->sc_unique == unique)
  215. goto retry;
  216. sc->sc_unique = unique;
  217. LIST_INSERT_HEAD(&pppoe_softc_list, sc, sc_list);
  218. splx(s);
  219. return (0);
  220. }
  221. /* Destroy a given interface. */
  222. int
  223. pppoe_clone_destroy(struct ifnet *ifp)
  224. {
  225. struct pppoe_softc *sc = ifp->if_softc;
  226. int s;
  227. s = splnet();
  228. LIST_REMOVE(sc, sc_list);
  229. timeout_del(&sc->sc_timeout);
  230. splx(s);
  231. sppp_detach(&sc->sc_sppp.pp_if);
  232. if_detach(ifp);
  233. if (sc->sc_concentrator_name)
  234. free(sc->sc_concentrator_name, M_DEVBUF, 0);
  235. if (sc->sc_service_name)
  236. free(sc->sc_service_name, M_DEVBUF, 0);
  237. if (sc->sc_ac_cookie)
  238. free(sc->sc_ac_cookie, M_DEVBUF, 0);
  239. if (sc->sc_relay_sid)
  240. free(sc->sc_relay_sid, M_DEVBUF, 0);
  241. free(sc, M_DEVBUF, 0);
  242. return (0);
  243. }
  244. /*
  245. * Find the interface handling the specified session.
  246. * Note: O(number of sessions open), this is a client-side only, mean
  247. * and lean implementation, so number of open sessions typically should
  248. * be 1.
  249. */
  250. static struct pppoe_softc *
  251. pppoe_find_softc_by_session(u_int session, u_int ifidx)
  252. {
  253. struct pppoe_softc *sc;
  254. if (session == 0)
  255. return (NULL);
  256. LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {
  257. if (sc->sc_state == PPPOE_STATE_SESSION
  258. && sc->sc_session == session
  259. && sc->sc_eth_if->if_index == ifidx) {
  260. return (sc);
  261. }
  262. }
  263. return (NULL);
  264. }
  265. /*
  266. * Check host unique token passed and return appropriate softc pointer,
  267. * or NULL if token is bogus.
  268. */
  269. static struct pppoe_softc *
  270. pppoe_find_softc_by_hunique(u_int8_t *token, size_t len, u_int ifidx)
  271. {
  272. struct pppoe_softc *sc;
  273. u_int32_t hunique;
  274. if (LIST_EMPTY(&pppoe_softc_list))
  275. return (NULL);
  276. if (len != sizeof(hunique))
  277. return (NULL);
  278. memcpy(&hunique, token, len);
  279. LIST_FOREACH(sc, &pppoe_softc_list, sc_list)
  280. if (sc->sc_unique == hunique)
  281. break;
  282. if (sc == NULL) {
  283. printf("pppoe: alien host unique tag, no session found\n");
  284. return (NULL);
  285. }
  286. /* should be safe to access *sc now */
  287. if (sc->sc_state < PPPOE_STATE_PADI_SENT || sc->sc_state >= PPPOE_STATE_SESSION) {
  288. printf("%s: host unique tag found, but it belongs to a connection in state %d\n",
  289. sc->sc_sppp.pp_if.if_xname, sc->sc_state);
  290. return (NULL);
  291. }
  292. if (sc->sc_eth_if->if_index != ifidx) {
  293. printf("%s: wrong interface, not accepting host unique\n",
  294. sc->sc_sppp.pp_if.if_xname);
  295. return (NULL);
  296. }
  297. return (sc);
  298. }
  299. /* Interface interrupt handler routine. */
  300. void
  301. pppoeintr(void)
  302. {
  303. struct mbuf *m;
  304. splsoftassert(IPL_SOFTNET);
  305. while ((m = niq_dequeue(&pppoediscinq)) != NULL)
  306. pppoe_disc_input(m);
  307. while ((m = niq_dequeue(&pppoeinq)) != NULL)
  308. pppoe_data_input(m);
  309. }
  310. /* Analyze and handle a single received packet while not in session state. */
  311. static void pppoe_dispatch_disc_pkt(struct mbuf *m, int off)
  312. {
  313. struct pppoe_softc *sc;
  314. struct pppoehdr *ph;
  315. struct pppoetag *pt;
  316. struct mbuf *n;
  317. struct ether_header *eh;
  318. const char *err_msg, *devname;
  319. size_t ac_cookie_len;
  320. size_t relay_sid_len;
  321. int noff, err, errortag;
  322. u_int16_t *max_payload;
  323. u_int16_t tag, len;
  324. u_int16_t session, plen;
  325. u_int8_t *ac_cookie;
  326. u_int8_t *relay_sid;
  327. u_int8_t code;
  328. #ifdef PPPOE_SERVER
  329. u_int8_t *hunique;
  330. size_t hunique_len;
  331. #endif
  332. err_msg = NULL;
  333. devname = "pppoe";
  334. errortag = 0;
  335. if (m->m_len < sizeof(*eh)) {
  336. m = m_pullup(m, sizeof(*eh));
  337. if (m == NULL)
  338. goto done;
  339. }
  340. eh = mtod(m, struct ether_header *);
  341. off += sizeof(*eh);
  342. ac_cookie = NULL;
  343. ac_cookie_len = 0;
  344. relay_sid = NULL;
  345. relay_sid_len = 0;
  346. max_payload = NULL;
  347. #ifdef PPPOE_SERVER
  348. hunique = NULL;
  349. hunique_len = 0;
  350. #endif
  351. session = 0;
  352. if (m->m_pkthdr.len - off <= PPPOE_HEADERLEN) {
  353. printf("pppoe: packet too short: %d\n", m->m_pkthdr.len);
  354. goto done;
  355. }
  356. n = m_pulldown(m, off, sizeof(*ph), &noff);
  357. if (n == NULL) {
  358. printf("pppoe: could not get PPPoE header\n");
  359. m = NULL;
  360. goto done;
  361. }
  362. ph = (struct pppoehdr *)(mtod(n, caddr_t) + noff);
  363. if (ph->vertype != PPPOE_VERTYPE) {
  364. printf("pppoe: unknown version/type packet: 0x%x\n",
  365. ph->vertype);
  366. goto done;
  367. }
  368. session = ntohs(ph->session);
  369. plen = ntohs(ph->plen);
  370. code = ph->code;
  371. off += sizeof(*ph);
  372. if (plen + off > m->m_pkthdr.len) {
  373. printf("pppoe: packet content does not fit: data available = %d, packet size = %u\n",
  374. m->m_pkthdr.len - off, plen);
  375. goto done;
  376. }
  377. /* ignore trailing garbage */
  378. m_adj(m, off + plen - m->m_pkthdr.len);
  379. tag = 0;
  380. len = 0;
  381. sc = NULL;
  382. while (off + sizeof(*pt) <= m->m_pkthdr.len) {
  383. n = m_pulldown(m, off, sizeof(*pt), &noff);
  384. if (n == NULL) {
  385. printf("%s: parse error\n", devname);
  386. m = NULL;
  387. goto done;
  388. }
  389. pt = (struct pppoetag *)(mtod(n, caddr_t) + noff);
  390. tag = ntohs(pt->tag);
  391. len = ntohs(pt->len);
  392. off += sizeof(*pt);
  393. if (off + len > m->m_pkthdr.len) {
  394. printf("%s: tag 0x%x len 0x%x is too long\n",
  395. devname, tag, len);
  396. goto done;
  397. }
  398. switch (tag) {
  399. case PPPOE_TAG_EOL:
  400. goto breakbreak;
  401. case PPPOE_TAG_SNAME:
  402. break; /* ignored */
  403. case PPPOE_TAG_ACNAME:
  404. break; /* ignored */
  405. case PPPOE_TAG_HUNIQUE:
  406. if (sc != NULL)
  407. break;
  408. n = m_pulldown(m, off, len, &noff);
  409. if (n == NULL) {
  410. m = NULL;
  411. err_msg = "TAG HUNIQUE ERROR";
  412. break;
  413. }
  414. #ifdef PPPOE_SERVER
  415. hunique = mtod(n, caddr_t) + noff;
  416. hunique_len = len;
  417. #endif
  418. sc = pppoe_find_softc_by_hunique(mtod(n, caddr_t) + noff,
  419. len, m->m_pkthdr.ph_ifidx);
  420. if (sc != NULL)
  421. devname = sc->sc_sppp.pp_if.if_xname;
  422. break;
  423. case PPPOE_TAG_ACCOOKIE:
  424. if (ac_cookie == NULL) {
  425. n = m_pulldown(m, off, len,
  426. &noff);
  427. if (n == NULL) {
  428. err_msg = "TAG ACCOOKIE ERROR";
  429. m = NULL;
  430. break;
  431. }
  432. ac_cookie = mtod(n, caddr_t) + noff;
  433. ac_cookie_len = len;
  434. }
  435. break;
  436. case PPPOE_TAG_RELAYSID:
  437. if (relay_sid == NULL) {
  438. n = m_pulldown(m, off, len,
  439. &noff);
  440. if (n == NULL) {
  441. err_msg = "TAG RELAYSID ERROR";
  442. m = NULL;
  443. break;
  444. }
  445. relay_sid = mtod(n, caddr_t) + noff;
  446. relay_sid_len = len;
  447. }
  448. break;
  449. case PPPOE_TAG_MAX_PAYLOAD:
  450. if (max_payload == NULL) {
  451. n = m_pulldown(m, off, len,
  452. &noff);
  453. if (n == NULL || len != 2) {
  454. err_msg = "TAG MAX_PAYLOAD ERROR";
  455. m = NULL;
  456. break;
  457. }
  458. max_payload = (u_int16_t *)(mtod(n, caddr_t) + noff);
  459. }
  460. break;
  461. case PPPOE_TAG_SNAME_ERR:
  462. err_msg = "SERVICE NAME ERROR";
  463. errortag = 1;
  464. break;
  465. case PPPOE_TAG_ACSYS_ERR:
  466. err_msg = "AC SYSTEM ERROR";
  467. errortag = 1;
  468. break;
  469. case PPPOE_TAG_GENERIC_ERR:
  470. err_msg = "GENERIC ERROR";
  471. errortag = 1;
  472. break;
  473. }
  474. if (err_msg) {
  475. log(LOG_INFO, "%s: %s: ", devname, err_msg);
  476. if (errortag && len) {
  477. n = m_pulldown(m, off, len,
  478. &noff);
  479. if (n) {
  480. u_int8_t *et = mtod(n, caddr_t) + noff;
  481. while (len--)
  482. addlog("%c", *et++);
  483. }
  484. }
  485. addlog("\n");
  486. goto done;
  487. }
  488. off += len;
  489. }
  490. breakbreak:
  491. switch (code) {
  492. case PPPOE_CODE_PADI:
  493. #ifdef PPPOE_SERVER
  494. /*
  495. * Got service name, concentrator name, and/or host unique.
  496. * Ignore if we have no interfaces with IFF_PASSIVE|IFF_UP.
  497. */
  498. if (LIST_EMPTY(&pppoe_softc_list))
  499. goto done;
  500. LIST_FOREACH(sc, &pppoe_softc_list, sc_list) {
  501. if (!(sc->sc_sppp.pp_if.if_flags & IFF_UP))
  502. continue;
  503. if (!(sc->sc_sppp.pp_if.if_flags & IFF_PASSIVE))
  504. continue;
  505. if (sc->sc_state == PPPOE_STATE_INITIAL)
  506. break;
  507. }
  508. if (sc == NULL) {
  509. #ifdef PPPOE_DEBUG
  510. printf("pppoe: free passive interface is not found\n");
  511. #endif
  512. goto done;
  513. }
  514. if (hunique) {
  515. if (sc->sc_hunique)
  516. free(sc->sc_hunique, M_DEVBUF, 0);
  517. sc->sc_hunique = malloc(hunique_len, M_DEVBUF,
  518. M_DONTWAIT);
  519. if (sc->sc_hunique == NULL)
  520. goto done;
  521. sc->sc_hunique_len = hunique_len;
  522. memcpy(sc->sc_hunique, hunique, hunique_len);
  523. }
  524. memcpy(&sc->sc_dest, eh->ether_shost, sizeof(sc->sc_dest));
  525. sc->sc_state = PPPOE_STATE_PADO_SENT;
  526. pppoe_send_pado(sc);
  527. break;
  528. #endif /* PPPOE_SERVER */
  529. case PPPOE_CODE_PADR:
  530. #ifdef PPPOE_SERVER
  531. /*
  532. * Get sc from ac_cookie if IFF_PASSIVE.
  533. */
  534. if (ac_cookie == NULL) {
  535. /* be quiet if there is not a single pppoe instance */
  536. printf("pppoe: received PADR but not includes ac_cookie\n");
  537. goto done;
  538. }
  539. sc = pppoe_find_softc_by_hunique(ac_cookie,
  540. ac_cookie_len,
  541. m->m_pkthdr.ph_ifidx);
  542. if (sc == NULL) {
  543. /* be quiet if there is not a single pppoe instance */
  544. if (!LIST_EMPTY(&pppoe_softc_list))
  545. printf("pppoe: received PADR but could not find request for it\n");
  546. goto done;
  547. }
  548. if (sc->sc_state != PPPOE_STATE_PADO_SENT) {
  549. printf("%s: received unexpected PADR\n",
  550. sc->sc_sppp.pp_if.if_xname);
  551. goto done;
  552. }
  553. if (hunique) {
  554. if (sc->sc_hunique)
  555. free(sc->sc_hunique, M_DEVBUF, 0);
  556. sc->sc_hunique = malloc(hunique_len, M_DEVBUF,
  557. M_DONTWAIT);
  558. if (sc->sc_hunique == NULL)
  559. goto done;
  560. sc->sc_hunique_len = hunique_len;
  561. memcpy(sc->sc_hunique, hunique, hunique_len);
  562. }
  563. pppoe_send_pads(sc);
  564. sc->sc_state = PPPOE_STATE_SESSION;
  565. sc->sc_sppp.pp_up(&sc->sc_sppp);
  566. break;
  567. #else
  568. /* ignore, we are no access concentrator */
  569. goto done;
  570. #endif /* PPPOE_SERVER */
  571. case PPPOE_CODE_PADO:
  572. if (sc == NULL) {
  573. /* be quiet if there is not a single pppoe instance */
  574. if (!LIST_EMPTY(&pppoe_softc_list))
  575. printf("pppoe: received PADO but could not find request for it\n");
  576. goto done;
  577. }
  578. if (sc->sc_state != PPPOE_STATE_PADI_SENT) {
  579. printf("%s: received unexpected PADO\n",
  580. sc->sc_sppp.pp_if.if_xname);
  581. goto done;
  582. }
  583. if (ac_cookie) {
  584. if (sc->sc_ac_cookie)
  585. free(sc->sc_ac_cookie, M_DEVBUF, 0);
  586. sc->sc_ac_cookie = malloc(ac_cookie_len, M_DEVBUF,
  587. M_DONTWAIT);
  588. if (sc->sc_ac_cookie == NULL)
  589. goto done;
  590. sc->sc_ac_cookie_len = ac_cookie_len;
  591. memcpy(sc->sc_ac_cookie, ac_cookie, ac_cookie_len);
  592. }
  593. if (relay_sid) {
  594. if (sc->sc_relay_sid)
  595. free(sc->sc_relay_sid, M_DEVBUF, 0);
  596. sc->sc_relay_sid = malloc(relay_sid_len, M_DEVBUF,
  597. M_DONTWAIT);
  598. if (sc->sc_relay_sid == NULL)
  599. goto done;
  600. sc->sc_relay_sid_len = relay_sid_len;
  601. memcpy(sc->sc_relay_sid, relay_sid, relay_sid_len);
  602. }
  603. if (sc->sc_sppp.pp_if.if_mtu > PPPOE_MTU &&
  604. (max_payload == NULL ||
  605. ntohs(*max_payload) != sc->sc_sppp.pp_if.if_mtu)) {
  606. printf("%s: No valid PPP-Max-Payload tag received in PADO\n",
  607. sc->sc_sppp.pp_if.if_xname);
  608. sc->sc_sppp.pp_if.if_mtu = PPPOE_MTU;
  609. }
  610. memcpy(&sc->sc_dest, eh->ether_shost, sizeof(sc->sc_dest));
  611. sc->sc_padr_retried = 0;
  612. sc->sc_state = PPPOE_STATE_PADR_SENT;
  613. if ((err = pppoe_send_padr(sc)) != 0) {
  614. PPPOEDEBUG(("%s: failed to send PADR, error=%d\n",
  615. sc->sc_sppp.pp_if.if_xname, err));
  616. }
  617. timeout_add(&sc->sc_timeout,
  618. PPPOE_DISC_TIMEOUT * (1 + sc->sc_padr_retried));
  619. break;
  620. case PPPOE_CODE_PADS:
  621. if (sc == NULL)
  622. goto done;
  623. sc->sc_session = session;
  624. timeout_del(&sc->sc_timeout);
  625. PPPOEDEBUG(("%s: session 0x%x connected\n",
  626. sc->sc_sppp.pp_if.if_xname, session));
  627. sc->sc_state = PPPOE_STATE_SESSION;
  628. microtime(&sc->sc_session_time);
  629. sc->sc_sppp.pp_up(&sc->sc_sppp); /* notify upper layers */
  630. break;
  631. case PPPOE_CODE_PADT:
  632. if (sc == NULL)
  633. goto done;
  634. /* stop timer (we might be about to transmit a PADT ourself) */
  635. timeout_del(&sc->sc_timeout);
  636. PPPOEDEBUG(("%s: session 0x%x terminated, received PADT\n",
  637. sc->sc_sppp.pp_if.if_xname, session));
  638. /* clean up softc */
  639. sc->sc_state = PPPOE_STATE_INITIAL;
  640. memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest));
  641. if (sc->sc_ac_cookie) {
  642. free(sc->sc_ac_cookie, M_DEVBUF, 0);
  643. sc->sc_ac_cookie = NULL;
  644. }
  645. if (sc->sc_relay_sid) {
  646. free(sc->sc_relay_sid, M_DEVBUF, 0);
  647. sc->sc_relay_sid = NULL;
  648. }
  649. sc->sc_ac_cookie_len = 0;
  650. sc->sc_relay_sid_len = 0;
  651. sc->sc_session = 0;
  652. sc->sc_session_time.tv_sec = 0;
  653. sc->sc_session_time.tv_usec = 0;
  654. sc->sc_sppp.pp_down(&sc->sc_sppp); /* signal upper layer */
  655. break;
  656. default:
  657. printf("%s: unknown code (0x%04x) session = 0x%04x\n",
  658. sc ? sc->sc_sppp.pp_if.if_xname : "pppoe",
  659. code, session);
  660. break;
  661. }
  662. done:
  663. m_freem(m);
  664. }
  665. /* Input function for discovery packets. */
  666. static void
  667. pppoe_disc_input(struct mbuf *m)
  668. {
  669. /* avoid error messages if there is not a single pppoe instance */
  670. if (!LIST_EMPTY(&pppoe_softc_list)) {
  671. KASSERT(m->m_flags & M_PKTHDR);
  672. pppoe_dispatch_disc_pkt(m, 0);
  673. } else
  674. m_freem(m);
  675. }
  676. /* Input function for data packets */
  677. static void
  678. pppoe_data_input(struct mbuf *m)
  679. {
  680. struct pppoe_softc *sc;
  681. struct pppoehdr *ph;
  682. u_int16_t session, plen;
  683. #ifdef PPPOE_TERM_UNKNOWN_SESSIONS
  684. u_int8_t shost[ETHER_ADDR_LEN];
  685. #endif
  686. if (LIST_EMPTY(&pppoe_softc_list))
  687. goto drop;
  688. KASSERT(m->m_flags & M_PKTHDR);
  689. #ifdef PPPOE_TERM_UNKNOWN_SESSIONS
  690. memcpy(shost, mtod(m, struct ether_header*)->ether_shost, ETHER_ADDR_LEN);
  691. #endif
  692. m_adj(m, sizeof(struct ether_header));
  693. if (m->m_pkthdr.len <= PPPOE_HEADERLEN) {
  694. printf("pppoe (data): dropping too short packet: %d bytes\n",
  695. m->m_pkthdr.len);
  696. goto drop;
  697. }
  698. if (m->m_len < sizeof(*ph)) {
  699. m = m_pullup(m, sizeof(*ph));
  700. if (m == NULL) {
  701. printf("pppoe (data): could not get PPPoE header\n");
  702. return;
  703. }
  704. }
  705. ph = mtod(m, struct pppoehdr *);
  706. if (ph->vertype != PPPOE_VERTYPE) {
  707. printf("pppoe (data): unknown version/type packet: 0x%x\n",
  708. ph->vertype);
  709. goto drop;
  710. }
  711. if (ph->code != 0)
  712. goto drop;
  713. session = ntohs(ph->session);
  714. sc = pppoe_find_softc_by_session(session, m->m_pkthdr.ph_ifidx);
  715. if (sc == NULL) {
  716. #ifdef PPPOE_TERM_UNKNOWN_SESSIONS
  717. printf("pppoe (data): input for unknown session 0x%x, sending PADT\n",
  718. session);
  719. pppoe_send_padt(m->m_pkthdr.rcvif, session, shost);
  720. #endif
  721. goto drop;
  722. }
  723. plen = ntohs(ph->plen);
  724. #if NBPFILTER > 0
  725. if(sc->sc_sppp.pp_if.if_bpf)
  726. bpf_mtap(sc->sc_sppp.pp_if.if_bpf, m, BPF_DIRECTION_IN);
  727. #endif
  728. m_adj(m, PPPOE_HEADERLEN);
  729. #ifdef PPPOE_DEBUG
  730. {
  731. struct mbuf *p;
  732. printf("%s: pkthdr.len=%d, pppoe.len=%d",
  733. sc->sc_sppp.pp_if.if_xname,
  734. m->m_pkthdr.len, plen);
  735. p = m;
  736. while (p) {
  737. printf(" l=%d", p->m_len);
  738. p = p->m_next;
  739. }
  740. printf("\n");
  741. }
  742. #endif
  743. if (m->m_pkthdr.len < plen)
  744. goto drop;
  745. /* fix incoming interface pointer (not the raw ethernet interface anymore) */
  746. m->m_pkthdr.ph_ifidx = sc->sc_sppp.pp_if.if_index;
  747. /* pass packet up and account for it */
  748. sc->sc_sppp.pp_if.if_ipackets++;
  749. sppp_input(&sc->sc_sppp.pp_if, m);
  750. return;
  751. drop:
  752. m_freem(m);
  753. }
  754. static int
  755. pppoe_output(struct pppoe_softc *sc, struct mbuf *m)
  756. {
  757. struct sockaddr dst;
  758. struct ether_header *eh;
  759. u_int16_t etype;
  760. if (sc->sc_eth_if == NULL) {
  761. m_freem(m);
  762. return (EIO);
  763. }
  764. if ((sc->sc_eth_if->if_flags & (IFF_UP|IFF_RUNNING))
  765. != (IFF_UP|IFF_RUNNING)) {
  766. m_freem(m);
  767. return (ENETDOWN);
  768. }
  769. memset(&dst, 0, sizeof dst);
  770. dst.sa_family = AF_UNSPEC;
  771. eh = (struct ether_header*)&dst.sa_data;
  772. etype = sc->sc_state == PPPOE_STATE_SESSION ? ETHERTYPE_PPPOE : ETHERTYPE_PPPOEDISC;
  773. eh->ether_type = htons(etype);
  774. memcpy(&eh->ether_dhost, &sc->sc_dest, sizeof sc->sc_dest);
  775. PPPOEDEBUG(("%s (%x) state=%d, session=0x%x output -> %s, len=%d\n",
  776. sc->sc_sppp.pp_if.if_xname, etype,
  777. sc->sc_state, sc->sc_session,
  778. ether_sprintf((unsigned char *)&sc->sc_dest), m->m_pkthdr.len));
  779. m->m_flags &= ~(M_BCAST|M_MCAST);
  780. /* encapsulated packet is forced into rdomain of physical interface */
  781. m->m_pkthdr.ph_rtableid = sc->sc_eth_if->if_rdomain;
  782. sc->sc_sppp.pp_if.if_opackets++;
  783. return (sc->sc_eth_if->if_output(sc->sc_eth_if, m, &dst, NULL));
  784. }
  785. /* The ioctl routine. */
  786. static int
  787. pppoe_ioctl(struct ifnet *ifp, unsigned long cmd, caddr_t data)
  788. {
  789. struct proc *p = curproc; /* XXX */
  790. struct pppoe_softc *sc = (struct pppoe_softc *)ifp;
  791. int error = 0;
  792. switch (cmd) {
  793. case PPPOESETPARMS:
  794. {
  795. struct pppoediscparms *parms = (struct pppoediscparms *)data;
  796. int len;
  797. if ((error = suser(p, 0)) != 0)
  798. return (error);
  799. if (parms->eth_ifname[0] != '\0') {
  800. struct ifnet *eth_if;
  801. eth_if = ifunit(parms->eth_ifname);
  802. if (eth_if == NULL || eth_if->if_type != IFT_ETHER) {
  803. sc->sc_eth_if = NULL;
  804. return (ENXIO);
  805. }
  806. if (sc->sc_sppp.pp_if.if_mtu >
  807. eth_if->if_mtu - PPPOE_OVERHEAD) {
  808. sc->sc_sppp.pp_if.if_mtu = eth_if->if_mtu -
  809. PPPOE_OVERHEAD;
  810. }
  811. sc->sc_eth_if = eth_if;
  812. }
  813. if (sc->sc_concentrator_name)
  814. free(sc->sc_concentrator_name, M_DEVBUF, 0);
  815. sc->sc_concentrator_name = NULL;
  816. len = strlen(parms->ac_name);
  817. if (len > 0 && len < sizeof(parms->ac_name)) {
  818. char *p = malloc(len + 1, M_DEVBUF, M_WAITOK|M_CANFAIL);
  819. if (p == NULL)
  820. return (ENOMEM);
  821. strlcpy(p, parms->ac_name, len + 1);
  822. sc->sc_concentrator_name = p;
  823. }
  824. if (sc->sc_service_name)
  825. free(sc->sc_service_name, M_DEVBUF, 0);
  826. sc->sc_service_name = NULL;
  827. len = strlen(parms->service_name);
  828. if (len > 0 && len < sizeof(parms->service_name)) {
  829. char *p = malloc(len + 1, M_DEVBUF, M_WAITOK|M_CANFAIL);
  830. if (p == NULL)
  831. return (ENOMEM);
  832. strlcpy(p, parms->service_name, len + 1);
  833. sc->sc_service_name = p;
  834. }
  835. return (0);
  836. }
  837. break;
  838. case PPPOEGETPARMS:
  839. {
  840. struct pppoediscparms *parms = (struct pppoediscparms *)data;
  841. if (sc->sc_eth_if)
  842. strlcpy(parms->eth_ifname, sc->sc_eth_if->if_xname,
  843. IFNAMSIZ);
  844. else
  845. parms->eth_ifname[0] = '\0';
  846. if (sc->sc_concentrator_name)
  847. strlcpy(parms->ac_name, sc->sc_concentrator_name,
  848. sizeof(parms->ac_name));
  849. else
  850. parms->ac_name[0] = '\0';
  851. if (sc->sc_service_name)
  852. strlcpy(parms->service_name, sc->sc_service_name,
  853. sizeof(parms->service_name));
  854. else
  855. parms->service_name[0] = '\0';
  856. return (0);
  857. }
  858. break;
  859. case PPPOEGETSESSION:
  860. {
  861. struct pppoeconnectionstate *state =
  862. (struct pppoeconnectionstate *)data;
  863. state->state = sc->sc_state;
  864. state->session_id = sc->sc_session;
  865. state->padi_retry_no = sc->sc_padi_retried;
  866. state->padr_retry_no = sc->sc_padr_retried;
  867. state->session_time.tv_sec = sc->sc_session_time.tv_sec;
  868. state->session_time.tv_usec = sc->sc_session_time.tv_usec;
  869. return (0);
  870. }
  871. break;
  872. case SIOCSIFFLAGS:
  873. {
  874. struct ifreq *ifr = (struct ifreq *)data;
  875. /*
  876. * Prevent running re-establishment timers overriding
  877. * administrators choice.
  878. */
  879. if ((ifr->ifr_flags & IFF_UP) == 0
  880. && sc->sc_state >= PPPOE_STATE_PADI_SENT
  881. && sc->sc_state < PPPOE_STATE_SESSION) {
  882. timeout_del(&sc->sc_timeout);
  883. sc->sc_state = PPPOE_STATE_INITIAL;
  884. sc->sc_padi_retried = 0;
  885. sc->sc_padr_retried = 0;
  886. memcpy(&sc->sc_dest, etherbroadcastaddr,
  887. sizeof(sc->sc_dest));
  888. }
  889. return (sppp_ioctl(ifp, cmd, data));
  890. }
  891. case SIOCSIFMTU:
  892. {
  893. struct ifreq *ifr = (struct ifreq *)data;
  894. if (ifr->ifr_mtu > MIN(PPPOE_MAXMTU,
  895. (sc->sc_eth_if == NULL ? PPPOE_MAXMTU :
  896. (sc->sc_eth_if->if_mtu - PPPOE_OVERHEAD))))
  897. return (EINVAL);
  898. return (sppp_ioctl(ifp, cmd, data));
  899. }
  900. default:
  901. return (sppp_ioctl(ifp, cmd, data));
  902. }
  903. return (0);
  904. }
  905. /*
  906. * Allocate a mbuf/cluster with space to store the given data length
  907. * of payload, leaving space for prepending an ethernet header
  908. * in front.
  909. */
  910. static struct mbuf *
  911. pppoe_get_mbuf(size_t len)
  912. {
  913. struct mbuf *m;
  914. MGETHDR(m, M_DONTWAIT, MT_DATA);
  915. if (m == NULL)
  916. return (NULL);
  917. if (len + sizeof(struct ether_header) > MHLEN) {
  918. MCLGET(m, M_DONTWAIT);
  919. if ((m->m_flags & M_EXT) == 0) {
  920. m_free(m);
  921. return (NULL);
  922. }
  923. }
  924. m->m_data += sizeof(struct ether_header);
  925. m->m_len = len;
  926. m->m_pkthdr.len = len;
  927. m->m_pkthdr.ph_ifidx = 0;
  928. return (m);
  929. }
  930. /* Send PADI. */
  931. static int
  932. pppoe_send_padi(struct pppoe_softc *sc)
  933. {
  934. struct mbuf *m0;
  935. int len, l1 = 0, l2 = 0; /* XXX: gcc */
  936. u_int8_t *p;
  937. if (sc->sc_state > PPPOE_STATE_PADI_SENT)
  938. panic("pppoe_send_padi in state %d", sc->sc_state);
  939. /* calculate length of frame (excluding ethernet header + pppoe header) */
  940. len = 2 + 2 + 2 + 2 + sizeof(sc->sc_unique); /* service name tag is required, host unique is sent too */
  941. if (sc->sc_service_name != NULL) {
  942. l1 = strlen(sc->sc_service_name);
  943. len += l1;
  944. }
  945. if (sc->sc_concentrator_name != NULL) {
  946. l2 = strlen(sc->sc_concentrator_name);
  947. len += 2 + 2 + l2;
  948. }
  949. if (sc->sc_sppp.pp_if.if_mtu > PPPOE_MTU)
  950. len += 2 + 2 + 2;
  951. /* allocate a buffer */
  952. m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN); /* header len + payload len */
  953. if (m0 == NULL)
  954. return (ENOBUFS);
  955. /* fill in pkt */
  956. p = mtod(m0, u_int8_t *);
  957. PPPOE_ADD_HEADER(p, PPPOE_CODE_PADI, 0, len);
  958. PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
  959. if (sc->sc_service_name != NULL) {
  960. PPPOE_ADD_16(p, l1);
  961. memcpy(p, sc->sc_service_name, l1);
  962. p += l1;
  963. } else {
  964. PPPOE_ADD_16(p, 0);
  965. }
  966. if (sc->sc_concentrator_name != NULL) {
  967. PPPOE_ADD_16(p, PPPOE_TAG_ACNAME);
  968. PPPOE_ADD_16(p, l2);
  969. memcpy(p, sc->sc_concentrator_name, l2);
  970. p += l2;
  971. }
  972. PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
  973. PPPOE_ADD_16(p, sizeof(sc->sc_unique));
  974. memcpy(p, &sc->sc_unique, sizeof(sc->sc_unique));
  975. p += sizeof(sc->sc_unique);
  976. if (sc->sc_sppp.pp_if.if_mtu > PPPOE_MTU) {
  977. PPPOE_ADD_16(p, PPPOE_TAG_MAX_PAYLOAD);
  978. PPPOE_ADD_16(p, 2);
  979. PPPOE_ADD_16(p, (u_int16_t)sc->sc_sppp.pp_if.if_mtu);
  980. }
  981. #ifdef PPPOE_DEBUG
  982. if (p - mtod(m0, u_int8_t *) != len + PPPOE_HEADERLEN)
  983. panic("pppoe_send_padi: garbled output len, should be %ld, is %ld",
  984. (long)(len + PPPOE_HEADERLEN), (long)(p - mtod(m0, u_int8_t *)));
  985. #endif
  986. /* send pkt */
  987. return (pppoe_output(sc, m0));
  988. }
  989. /* Watchdog function. */
  990. static void
  991. pppoe_timeout(void *arg)
  992. {
  993. struct pppoe_softc *sc = (struct pppoe_softc *)arg;
  994. int x, retry_wait, err;
  995. PPPOEDEBUG(("%s: timeout\n", sc->sc_sppp.pp_if.if_xname));
  996. switch (sc->sc_state) {
  997. case PPPOE_STATE_PADI_SENT:
  998. /*
  999. * We have two basic ways of retrying:
  1000. * - Quick retry mode: try a few times in short sequence
  1001. * - Slow retry mode: we already had a connection successfully
  1002. * established and will try infinitely (without user
  1003. * intervention)
  1004. * We only enter slow retry mode if IFF_LINK1 (aka autodial)
  1005. * is not set.
  1006. */
  1007. /* initialize for quick retry mode */
  1008. retry_wait = PPPOE_DISC_TIMEOUT * (1 + sc->sc_padi_retried);
  1009. x = splnet();
  1010. sc->sc_padi_retried++;
  1011. if (sc->sc_padi_retried >= PPPOE_DISC_MAXPADI) {
  1012. if ((sc->sc_sppp.pp_if.if_flags & IFF_LINK1) == 0) {
  1013. /* slow retry mode */
  1014. retry_wait = PPPOE_SLOW_RETRY;
  1015. } else {
  1016. pppoe_abort_connect(sc);
  1017. splx(x);
  1018. return;
  1019. }
  1020. }
  1021. if ((err = pppoe_send_padi(sc)) != 0) {
  1022. sc->sc_padi_retried--;
  1023. PPPOEDEBUG(("%s: failed to transmit PADI, error=%d\n",
  1024. sc->sc_sppp.pp_if.if_xname, err));
  1025. }
  1026. timeout_add(&sc->sc_timeout, retry_wait);
  1027. splx(x);
  1028. break;
  1029. case PPPOE_STATE_PADR_SENT:
  1030. x = splnet();
  1031. sc->sc_padr_retried++;
  1032. if (sc->sc_padr_retried >= PPPOE_DISC_MAXPADR) {
  1033. memcpy(&sc->sc_dest, etherbroadcastaddr,
  1034. sizeof(sc->sc_dest));
  1035. sc->sc_state = PPPOE_STATE_PADI_SENT;
  1036. sc->sc_padr_retried = 0;
  1037. if ((err = pppoe_send_padi(sc)) != 0) {
  1038. PPPOEDEBUG(("%s: failed to send PADI, error=%d\n",
  1039. sc->sc_sppp.pp_if.if_xname, err));
  1040. }
  1041. timeout_add(&sc->sc_timeout,
  1042. PPPOE_DISC_TIMEOUT * (1 + sc->sc_padi_retried));
  1043. splx(x);
  1044. return;
  1045. }
  1046. if ((err = pppoe_send_padr(sc)) != 0) {
  1047. sc->sc_padr_retried--;
  1048. PPPOEDEBUG(("%s: failed to send PADR, error=%d\n",
  1049. sc->sc_sppp.pp_if.if_xname, err));
  1050. }
  1051. timeout_add(&sc->sc_timeout,
  1052. PPPOE_DISC_TIMEOUT * (1 + sc->sc_padr_retried));
  1053. splx(x);
  1054. break;
  1055. case PPPOE_STATE_CLOSING:
  1056. pppoe_disconnect(sc);
  1057. break;
  1058. default:
  1059. return; /* all done, work in peace */
  1060. }
  1061. }
  1062. /* Start a connection (i.e. initiate discovery phase). */
  1063. static int
  1064. pppoe_connect(struct pppoe_softc *sc)
  1065. {
  1066. int x, err;
  1067. if (sc->sc_state != PPPOE_STATE_INITIAL)
  1068. return (EBUSY);
  1069. #ifdef PPPOE_SERVER
  1070. /* wait for PADI if IFF_PASSIVE */
  1071. if ((sc->sc_sppp.pp_if.if_flags & IFF_PASSIVE))
  1072. return (0);
  1073. #endif
  1074. x = splnet();
  1075. /* save state, in case we fail to send PADI */
  1076. sc->sc_state = PPPOE_STATE_PADI_SENT;
  1077. sc->sc_padr_retried = 0;
  1078. err = pppoe_send_padi(sc);
  1079. if (err != 0)
  1080. PPPOEDEBUG(("%s: failed to send PADI, error=%d\n",
  1081. sc->sc_sppp.pp_if.if_xname, err));
  1082. timeout_add(&sc->sc_timeout, PPPOE_DISC_TIMEOUT);
  1083. splx(x);
  1084. return (err);
  1085. }
  1086. /* disconnect */
  1087. static int
  1088. pppoe_disconnect(struct pppoe_softc *sc)
  1089. {
  1090. int err, x;
  1091. x = splnet();
  1092. if (sc->sc_state < PPPOE_STATE_SESSION)
  1093. err = EBUSY;
  1094. else {
  1095. PPPOEDEBUG(("%s: disconnecting\n",
  1096. sc->sc_sppp.pp_if.if_xname));
  1097. err = pppoe_send_padt(sc->sc_eth_if, sc->sc_session, (const u_int8_t *)&sc->sc_dest);
  1098. }
  1099. /* cleanup softc */
  1100. sc->sc_state = PPPOE_STATE_INITIAL;
  1101. memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest));
  1102. if (sc->sc_ac_cookie) {
  1103. free(sc->sc_ac_cookie, M_DEVBUF, 0);
  1104. sc->sc_ac_cookie = NULL;
  1105. }
  1106. sc->sc_ac_cookie_len = 0;
  1107. if (sc->sc_relay_sid) {
  1108. free(sc->sc_relay_sid, M_DEVBUF, 0);
  1109. sc->sc_relay_sid = NULL;
  1110. }
  1111. sc->sc_relay_sid_len = 0;
  1112. #ifdef PPPOE_SERVER
  1113. if (sc->sc_hunique) {
  1114. free(sc->sc_hunique, M_DEVBUF, 0);
  1115. sc->sc_hunique = NULL;
  1116. }
  1117. sc->sc_hunique_len = 0;
  1118. #endif
  1119. sc->sc_session = 0;
  1120. /* notify upper layer */
  1121. sc->sc_sppp.pp_down(&sc->sc_sppp);
  1122. splx(x);
  1123. return (err);
  1124. }
  1125. /* Connection attempt aborted. */
  1126. static void
  1127. pppoe_abort_connect(struct pppoe_softc *sc)
  1128. {
  1129. printf("%s: could not establish connection\n",
  1130. sc->sc_sppp.pp_if.if_xname);
  1131. sc->sc_state = PPPOE_STATE_CLOSING;
  1132. /* notify upper layer */
  1133. sc->sc_sppp.pp_down(&sc->sc_sppp);
  1134. /* clear connection state */
  1135. memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest));
  1136. sc->sc_state = PPPOE_STATE_INITIAL;
  1137. }
  1138. /* Send a PADR packet */
  1139. static int
  1140. pppoe_send_padr(struct pppoe_softc *sc)
  1141. {
  1142. struct mbuf *m0;
  1143. u_int8_t *p;
  1144. size_t len, l1 = 0; /* XXX: gcc */
  1145. if (sc->sc_state != PPPOE_STATE_PADR_SENT)
  1146. return (EIO);
  1147. len = 2 + 2 + 2 + 2 + sizeof(sc->sc_unique); /* service name, host unique */
  1148. if (sc->sc_service_name != NULL) { /* service name tag maybe empty */
  1149. l1 = strlen(sc->sc_service_name);
  1150. len += l1;
  1151. }
  1152. if (sc->sc_ac_cookie_len > 0)
  1153. len += 2 + 2 + sc->sc_ac_cookie_len; /* AC cookie */
  1154. if (sc->sc_relay_sid_len > 0)
  1155. len += 2 + 2 + sc->sc_relay_sid_len; /* Relay SID */
  1156. if (sc->sc_sppp.pp_if.if_mtu > PPPOE_MTU)
  1157. len += 2 + 2 + 2;
  1158. m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN);
  1159. if (m0 == NULL)
  1160. return (ENOBUFS);
  1161. p = mtod(m0, u_int8_t *);
  1162. PPPOE_ADD_HEADER(p, PPPOE_CODE_PADR, 0, len);
  1163. PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
  1164. if (sc->sc_service_name != NULL) {
  1165. PPPOE_ADD_16(p, l1);
  1166. memcpy(p, sc->sc_service_name, l1);
  1167. p += l1;
  1168. } else {
  1169. PPPOE_ADD_16(p, 0);
  1170. }
  1171. if (sc->sc_ac_cookie_len > 0) {
  1172. PPPOE_ADD_16(p, PPPOE_TAG_ACCOOKIE);
  1173. PPPOE_ADD_16(p, sc->sc_ac_cookie_len);
  1174. memcpy(p, sc->sc_ac_cookie, sc->sc_ac_cookie_len);
  1175. p += sc->sc_ac_cookie_len;
  1176. }
  1177. if (sc->sc_relay_sid_len > 0) {
  1178. PPPOE_ADD_16(p, PPPOE_TAG_RELAYSID);
  1179. PPPOE_ADD_16(p, sc->sc_relay_sid_len);
  1180. memcpy(p, sc->sc_relay_sid, sc->sc_relay_sid_len);
  1181. p += sc->sc_relay_sid_len;
  1182. }
  1183. PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
  1184. PPPOE_ADD_16(p, sizeof(sc->sc_unique));
  1185. memcpy(p, &sc->sc_unique, sizeof(sc->sc_unique));
  1186. p += sizeof(sc->sc_unique);
  1187. if (sc->sc_sppp.pp_if.if_mtu > PPPOE_MTU) {
  1188. PPPOE_ADD_16(p, PPPOE_TAG_MAX_PAYLOAD);
  1189. PPPOE_ADD_16(p, 2);
  1190. PPPOE_ADD_16(p, (u_int16_t)sc->sc_sppp.pp_if.if_mtu);
  1191. }
  1192. #ifdef PPPOE_DEBUG
  1193. if (p - mtod(m0, u_int8_t *) != len + PPPOE_HEADERLEN)
  1194. panic("pppoe_send_padr: garbled output len, should be %ld, is %ld",
  1195. (long)(len + PPPOE_HEADERLEN), (long)(p - mtod(m0, u_int8_t *)));
  1196. #endif
  1197. return (pppoe_output(sc, m0));
  1198. }
  1199. /* Send a PADT packet. */
  1200. static int
  1201. pppoe_send_padt(struct ifnet *outgoing_if, u_int session, const u_int8_t *dest)
  1202. {
  1203. struct ether_header *eh;
  1204. struct sockaddr dst;
  1205. struct mbuf *m0;
  1206. u_int8_t *p;
  1207. m0 = pppoe_get_mbuf(PPPOE_HEADERLEN);
  1208. if (m0 == NULL)
  1209. return (ENOBUFS);
  1210. p = mtod(m0, u_int8_t *);
  1211. PPPOE_ADD_HEADER(p, PPPOE_CODE_PADT, session, 0);
  1212. memset(&dst, 0, sizeof(dst));
  1213. dst.sa_family = AF_UNSPEC;
  1214. eh = (struct ether_header *)&dst.sa_data;
  1215. eh->ether_type = htons(ETHERTYPE_PPPOEDISC);
  1216. memcpy(&eh->ether_dhost, dest, ETHER_ADDR_LEN);
  1217. m0->m_flags &= ~(M_BCAST|M_MCAST);
  1218. /* encapsulated packet is forced into rdomain of physical interface */
  1219. m0->m_pkthdr.ph_rtableid = outgoing_if->if_rdomain;
  1220. return (outgoing_if->if_output(outgoing_if, m0, &dst, NULL));
  1221. }
  1222. #ifdef PPPOE_SERVER
  1223. /* Send a PADO packet. */
  1224. static int
  1225. pppoe_send_pado(struct pppoe_softc *sc)
  1226. {
  1227. struct mbuf *m0;
  1228. size_t len;
  1229. u_int8_t *p;
  1230. if (sc->sc_state != PPPOE_STATE_PADO_SENT)
  1231. return (EIO);
  1232. /* calc length */
  1233. len = 0;
  1234. /* include ac_cookie */
  1235. len += 2 + 2 + sizeof(sc->sc_unique);
  1236. /* include hunique */
  1237. len += 2 + 2 + sc->sc_hunique_len;
  1238. m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN);
  1239. if (m0 == NULL)
  1240. return (ENOBUFS);
  1241. p = mtod(m0, u_int8_t *);
  1242. PPPOE_ADD_HEADER(p, PPPOE_CODE_PADO, 0, len);
  1243. PPPOE_ADD_16(p, PPPOE_TAG_ACCOOKIE);
  1244. PPPOE_ADD_16(p, sizeof(sc->sc_unique));
  1245. memcpy(p, &sc, sizeof(sc->sc_unique));
  1246. p += sizeof(sc->sc_unique);
  1247. PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
  1248. PPPOE_ADD_16(p, sc->sc_hunique_len);
  1249. memcpy(p, sc->sc_hunique, sc->sc_hunique_len);
  1250. return (pppoe_output(sc, m0));
  1251. }
  1252. /* Send a PADS packet. */
  1253. static int
  1254. pppoe_send_pads(struct pppoe_softc *sc)
  1255. {
  1256. struct mbuf *m0;
  1257. size_t len, l1;
  1258. u_int8_t *p;
  1259. if (sc->sc_state != PPPOE_STATE_PADO_SENT)
  1260. return (EIO);
  1261. sc->sc_session = mono_time.tv_sec % 0xff + 1;
  1262. /* calc length */
  1263. len = 0;
  1264. /* include hunique */
  1265. len += 2 + 2 + 2 + 2 + sc->sc_hunique_len; /* service name, host unique */
  1266. if (sc->sc_service_name != NULL) { /* service name tag maybe empty */
  1267. l1 = strlen(sc->sc_service_name);
  1268. len += l1;
  1269. }
  1270. m0 = pppoe_get_mbuf(len + PPPOE_HEADERLEN);
  1271. if (m0 == NULL)
  1272. return (ENOBUFS);
  1273. p = mtod(m0, u_int8_t *);
  1274. PPPOE_ADD_HEADER(p, PPPOE_CODE_PADS, sc->sc_session, len);
  1275. PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
  1276. if (sc->sc_service_name != NULL) {
  1277. PPPOE_ADD_16(p, l1);
  1278. memcpy(p, sc->sc_service_name, l1);
  1279. p += l1;
  1280. } else {
  1281. PPPOE_ADD_16(p, 0);
  1282. }
  1283. PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
  1284. PPPOE_ADD_16(p, sc->sc_hunique_len);
  1285. memcpy(p, sc->sc_hunique, sc->sc_hunique_len);
  1286. return (pppoe_output(sc, m0));
  1287. }
  1288. #endif
  1289. /* this-layer-start function */
  1290. static void
  1291. pppoe_tls(struct sppp *sp)
  1292. {
  1293. struct pppoe_softc *sc = (void *)sp;
  1294. if (sc->sc_state != PPPOE_STATE_INITIAL)
  1295. return;
  1296. pppoe_connect(sc);
  1297. }
  1298. /* this-layer-finish function */
  1299. static void
  1300. pppoe_tlf(struct sppp *sp)
  1301. {
  1302. struct pppoe_softc *sc = (void *)sp;
  1303. if (sc->sc_state < PPPOE_STATE_SESSION)
  1304. return;
  1305. /*
  1306. * Do not call pppoe_disconnect here, the upper layer state
  1307. * machine gets confused by this. We must return from this
  1308. * function and defer disconnecting to the timeout handler.
  1309. */
  1310. sc->sc_state = PPPOE_STATE_CLOSING;
  1311. timeout_add(&sc->sc_timeout, hz / 50);
  1312. }
  1313. static void
  1314. pppoe_start(struct ifnet *ifp)
  1315. {
  1316. struct pppoe_softc *sc = (void *)ifp;
  1317. struct mbuf *m;
  1318. size_t len;
  1319. u_int8_t *p;
  1320. if (sppp_isempty(ifp))
  1321. return;
  1322. /* are we ready to process data yet? */
  1323. if (sc->sc_state < PPPOE_STATE_SESSION) {
  1324. sppp_flush(&sc->sc_sppp.pp_if);
  1325. return;
  1326. }
  1327. while ((m = sppp_dequeue(ifp)) != NULL) {
  1328. len = m->m_pkthdr.len;
  1329. M_PREPEND(m, PPPOE_HEADERLEN, M_DONTWAIT);
  1330. if (m == NULL) {
  1331. ifp->if_oerrors++;
  1332. continue;
  1333. }
  1334. p = mtod(m, u_int8_t *);
  1335. PPPOE_ADD_HEADER(p, 0, sc->sc_session, len);
  1336. #if NBPFILTER > 0
  1337. if(sc->sc_sppp.pp_if.if_bpf)
  1338. bpf_mtap(sc->sc_sppp.pp_if.if_bpf, m,
  1339. BPF_DIRECTION_OUT);
  1340. #endif
  1341. pppoe_output(sc, m);
  1342. }
  1343. }