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