if_pppoe.c revision 1.3 1 /* $NetBSD: if_pppoe.c,v 1.3 2001/06/18 12:32:47 martin Exp $ */
2
3 /*
4 * Copyright (c) 2001 Martin Husemann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 */
28
29 #include "pppoe.h"
30 #include "bpfilter.h"
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/callout.h>
36 #include <sys/malloc.h>
37 #include <sys/mbuf.h>
38 #include <sys/socket.h>
39 #include <sys/proc.h>
40 #include <sys/ioctl.h>
41 #include <net/if.h>
42 #include <net/if_types.h>
43 #include <net/if_ether.h>
44 #include <net/if_sppp.h>
45 #include <net/if_pppoe.h>
46
47 #if NBPFILTER > 0
48 #include <net/bpf.h>
49 #endif
50
51 #if NPPPOE > 0
52
53 #undef PPPOE_DEBUG /* XXX - remove this or make it an option */
54 /* #define PPPOE_DEBUG 1 */
55
56 #define PPPOE_HEADERLEN 6
57 #define PPPOE_VERTYPE 0x11 /* VER=1, TYPE = 1 */
58
59 #define PPPOE_TAG_EOL 0x0000 /* end of list */
60 #define PPPOE_TAG_SNAME 0x0101 /* service name */
61 #define PPPOE_TAG_ACNAME 0x0102 /* access concentrator name */
62 #define PPPOE_TAG_HUNIQUE 0x0103 /* host unique */
63 #define PPPOE_TAG_ACCOOKIE 0x0104 /* AC cookie */
64 #define PPPOE_TAG_VENDOR 0x0105 /* vendor specific */
65 #define PPPOE_TAG_RELAYSID 0x0110 /* relay session id */
66 #define PPPOE_TAG_SNAME_ERR 0x0201 /* service name error */
67 #define PPPOE_TAG_ACSYS_ERR 0x0202 /* AC system error */
68 #define PPPOE_TAG_GENERIC_ERR 0x0203 /* gerneric error */
69
70 #define PPPOE_CODE_PADI 0x09 /* Active Discovery Initiation */
71 #define PPPOE_CODE_PADO 0x07 /* Active Discovery Offer */
72 #define PPPOE_CODE_PADR 0x19 /* Active Discovery Request */
73 #define PPPOE_CODE_PADS 0x65 /* Active Discovery Session confirmation */
74 #define PPPOE_CODE_PADT 0xA7 /* Active Discovery Terminate */
75
76 /* Read a 16 bit unsigned value from a buffer */
77 #define PPPOE_READ_16(PTR, VAL) \
78 (VAL) = ((PTR)[0] << 8) | (PTR)[1]; \
79 (PTR)+=2
80
81 /* Add a 16 bit unsigned value to a buffer pointed to by PTR */
82 #define PPPOE_ADD_16(PTR, VAL) \
83 *(PTR)++ = (VAL) / 256; \
84 *(PTR)++ = (VAL) % 256
85
86 /* Add a complete PPPoE header to the buffer pointed to by PTR */
87 #define PPPOE_ADD_HEADER(PTR, CODE, SESS, LEN) \
88 *(PTR)++ = PPPOE_VERTYPE; \
89 *(PTR)++ = (CODE); \
90 PPPOE_ADD_16(PTR, SESS); \
91 PPPOE_ADD_16(PTR, LEN)
92
93 struct pppoe_softc {
94 struct sppp sc_sppp; /* contains a struct ifnet as first element */
95 struct ifnet *sc_eth_if; /* ethernet interface we are using */
96
97 #define PPPOE_DISC_TIMEOUT hz/5
98 #define PPPOE_DISC_MAXPADI 4 /* retry PADI four times */
99 #define PPPOE_DISC_MAXPADR 2 /* retry PADR twice */
100
101 #define PPPOE_STATE_INITIAL 0
102 #define PPPOE_STATE_PADI_SENT 1
103 #define PPPOE_STATE_PADR_SENT 2
104 #define PPPOE_STATE_SESSION 3
105 #define PPPOE_STATE_CLOSING 4
106 int sc_state; /* discovery phase or session connected */
107 struct ether_addr sc_dest; /* hardware address of concentrator */
108 u_int16_t sc_session; /* PPPoE session id */
109
110 char *sc_service_name; /* if != NULL: requested name of service */
111 char *sc_concentrator_name; /* if != NULL: requested concentrator id */
112 u_int8_t *sc_ac_cookie; /* content of AC cookie we must echo back */
113 size_t sc_ac_cookie_len; /* length of cookie data */
114 struct callout sc_timeout; /* timeout while not in session state */
115 int sc_padi_retried; /* number of PADI retries already done */
116 int sc_padr_retried; /* number of PADR retries already done */
117 };
118
119 /* incoming traffic will be queued here */
120 struct ifqueue ppoediscinq = { NULL };
121 struct ifqueue ppoeinq = { NULL };
122
123 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
124 void * pppoe_softintr = NULL;
125 static void pppoe_softintr_handler(void *);
126 #else
127 struct callout pppoe_softintr = CALLOUT_INITIALIZER;
128 void pppoe_softintr_handler(void*);
129 #endif
130
131 extern int sppp_ioctl(struct ifnet *ifp, unsigned long cmd, void *data);
132
133 /* input routines */
134 static void pppoe_input(void);
135 static void pppoe_disc_input(struct mbuf *m);
136 static void pppoe_dispatch_disc_pkt(u_int8_t *p, size_t size, struct ifnet *rcvif, struct ether_header *eh);
137 static void pppoe_data_input(struct mbuf *m);
138
139 /* management routines */
140 void pppoeattach(void);
141 static int pppoe_connect(struct pppoe_softc *sc);
142 static int pppoe_disconnect(struct pppoe_softc *sc);
143 static void pppoe_abort_connect(struct pppoe_softc *sc);
144 static int pppoe_ioctl(struct ifnet *ifp, unsigned long cmd, caddr_t data);
145 static void pppoe_tls(struct sppp *sp);
146 static void pppoe_tlf(struct sppp *sp);
147 static void pppoe_start(struct ifnet *ifp);
148
149 /* internal timeout handling */
150 static void pppoe_timeout(void*);
151
152 /* sending actual protocol controll packets */
153 static int pppoe_send_padi(struct pppoe_softc *sc);
154 static int pppoe_send_padr(struct pppoe_softc *sc);
155 static int pppoe_send_padt(struct pppoe_softc *sc);
156
157 /* raw output */
158 static int pppoe_output(struct pppoe_softc *sc, struct mbuf *m);
159
160 /* internal helper functions */
161 static struct pppoe_softc * pppoe_find_softc_by_session(u_int session, struct ifnet *rcvif);
162 static struct pppoe_softc * pppoe_find_softc_by_hunique(u_int8_t *token, size_t len, struct ifnet *rcvif);
163
164 /* XXX - turn into SLIST and implement "ifconfig create" */
165 struct pppoe_softc pppoe_softc[NPPPOE];
166
167 /*
168 * Initialize device data structures
169 */
170 void
171 pppoeattach()
172 {
173 struct pppoe_softc *sc;
174 int i = 0;
175
176 memset(pppoe_softc, 0, sizeof pppoe_softc);
177 for (sc = pppoe_softc; i < NPPPOE; sc++) {
178 sprintf(sc->sc_sppp.pp_if.if_xname, "pppoe%d", i++);
179 sc->sc_sppp.pp_if.if_softc = sc;
180 sc->sc_sppp.pp_if.if_mtu = ETHERMTU - PPPOE_HEADERLEN;
181 sc->sc_sppp.pp_if.if_flags = IFF_SIMPLEX | IFF_POINTOPOINT
182 | IFF_MULTICAST | IFF_LINK1; /* auto "dial" */
183 sc->sc_sppp.pp_if.if_type = IFT_PPP;
184 sc->sc_sppp.pp_if.if_hdrlen = sizeof(struct ether_header)+PPPOE_HEADERLEN;
185 sc->sc_sppp.pp_if.if_dlt = DLT_PPP_ETHER;
186 sc->sc_sppp.pp_flags |= PP_NOFRAMING; /* no serial encapsulation */
187 sc->sc_sppp.pp_if.if_ioctl = pppoe_ioctl;
188 IFQ_SET_MAXLEN(&sc->sc_sppp.pp_if.if_snd, IFQ_MAXLEN);
189 IFQ_SET_READY(&sc->sc_sppp.pp_if.if_snd);
190
191 /* changed to real address later */
192 memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest));
193
194 callout_init(&sc->sc_timeout);
195
196 sc->sc_sppp.pp_if.if_start = pppoe_start;
197 sc->sc_sppp.pp_tls = pppoe_tls;
198 sc->sc_sppp.pp_tlf = pppoe_tlf;
199 sc->sc_sppp.pp_framebytes = PPPOE_HEADERLEN; /* framing added to ppp packets */
200
201 if_attach(&sc->sc_sppp.pp_if);
202 sppp_attach(&sc->sc_sppp.pp_if);
203
204 if_alloc_sadl(&sc->sc_sppp.pp_if);
205 #if NBPFILTER > 0
206 bpfattach(&sc->sc_sppp.pp_if, DLT_PPP_ETHER, 0);
207 #endif
208 }
209
210 ppoediscinq.ifq_maxlen = IFQ_MAXLEN;
211 ppoeinq.ifq_maxlen = IFQ_MAXLEN;
212
213 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
214 pppoe_softintr = softintr_establish(IPL_SOFTNET, pppoe_softintr_handler, NULL);
215 #endif
216 }
217
218 /*
219 * Find the interface handling the specified session.
220 * Note: O(number of sessions open), this is a client-side only, mean
221 * and lean implementation, so number of open sessions typically should
222 * be 1.
223 */
224 static struct pppoe_softc *
225 pppoe_find_softc_by_session(u_int session, struct ifnet *rcvif)
226 {
227 int i;
228
229 if (session == 0) return NULL;
230
231 for (i = 0; i < NPPPOE; i++) {
232 if (pppoe_softc[i].sc_state == PPPOE_STATE_SESSION
233 && pppoe_softc[i].sc_session == session) {
234 if (pppoe_softc[i].sc_eth_if == rcvif)
235 return &pppoe_softc[i];
236 else
237 return NULL;
238 }
239 }
240 return NULL;
241 }
242
243 /* Check host unique token passed and return appropriate softc pointer,
244 * or NULL if token is bogus. */
245 static struct pppoe_softc *
246 pppoe_find_softc_by_hunique(u_int8_t *token, size_t len, struct ifnet *rcvif)
247 {
248 struct pppoe_softc *sc = NULL;
249 if (len != sizeof sc) return NULL;
250 memcpy(&sc, token, len);
251 /* XXX - change when turning softc array into an SLIST */
252 if (sc < &pppoe_softc[0] && sc > &pppoe_softc[NPPPOE-1]) {
253 #ifdef PPPOE_DEBUG
254 printf("pppoe: invalid host unique value\n");
255 #endif
256 return NULL;
257 }
258 /* should be safe to access *sc now */
259 if (sc->sc_state < PPPOE_STATE_PADI_SENT || sc->sc_state >= PPPOE_STATE_SESSION) {
260 #ifdef PPPOE_DEBUG
261 printf("%s: state=%d, not accepting host unique\n",
262 sc->sc_sppp.pp_if.if_xname, sc->sc_state);
263 #endif
264 return NULL;
265 }
266 if (sc->sc_eth_if != rcvif) {
267 #ifdef PPPOE_DEBUG
268 printf("%s: wrong interface, not accepting host unique\n",
269 sc->sc_sppp.pp_if.if_xname);
270 #endif
271 return NULL;
272 }
273 return sc;
274 }
275
276 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
277 static void pppoe_softintr_handler(void *dummy)
278 {
279 /* called at splsoftnet() */
280 pppoe_input();
281 }
282 #else
283 void pppoe_softintr_handler(void *dummy)
284 {
285 int s = splnet();
286 pppoe_input();
287 callout_deactivate(&pppoe_softintr);
288 splx(s);
289 }
290 #endif
291
292 /* called at appropriate protection level */
293 static void
294 pppoe_input()
295 {
296 struct mbuf *m;
297 int s, disc_done, data_done;
298
299 do {
300 disc_done = 0;
301 data_done = 0;
302 for (;;) {
303 s = splnet();
304 IF_DEQUEUE(&ppoediscinq, m);
305 splx(s);
306 if (m == NULL) break;
307 disc_done = 1;
308 pppoe_disc_input(m);
309 }
310
311 for (;;) {
312 s = splnet();
313 IF_DEQUEUE(&ppoeinq, m);
314 splx(s);
315 if (m == NULL) break;
316 data_done = 1;
317 pppoe_data_input(m);
318 }
319 } while (disc_done || data_done);
320 }
321
322 /* analyze and handle a single received packet while not in session state */
323 static void pppoe_dispatch_disc_pkt(u_int8_t *p, size_t size, struct ifnet *rcvif, struct ether_header *eh)
324 {
325 u_int16_t tag, len;
326 u_int8_t vertype, code;
327 u_int16_t session, plen;
328 struct pppoe_softc *sc;
329 const char *err_msg = NULL;
330 u_int8_t * ac_cookie;
331 size_t ac_cookie_len;
332
333 ac_cookie = NULL;
334 ac_cookie_len = 0;
335 session = 0;
336 if (size <= PPPOE_HEADERLEN) {
337 printf("pppoe: packet too short: %ld\n", (long)size);
338 return;
339 }
340 vertype = *p++;
341 if (vertype != PPPOE_VERTYPE) {
342 printf("pppoe: unknown version/type packet: 0x%x\n", vertype);
343 return;
344 }
345 code = *p++;
346 PPPOE_READ_16(p, session);
347 PPPOE_READ_16(p, plen);
348 size -= PPPOE_HEADERLEN;
349
350 if (plen > size) {
351 printf("pppoe: packet content does not fit: data available = %ld, packet size = %ld\n",
352 (long)size, (long)plen);
353 return;
354 }
355 size = plen; /* ignore trailing garbage */
356 tag = 0;
357 len = 0;
358 sc = NULL;
359 while (size > 4) {
360 PPPOE_READ_16(p, tag);
361 PPPOE_READ_16(p, len);
362 if (len > size) {
363 printf("pppoe: tag 0x%x len 0x%x is too long\n", tag, len);
364 return;
365 }
366 switch (tag) {
367 case PPPOE_TAG_EOL:
368 size = 0; break;
369 case PPPOE_TAG_SNAME:
370 break; /* ignored */
371 case PPPOE_TAG_ACNAME:
372 break; /* ignored */
373 case PPPOE_TAG_HUNIQUE:
374 if (sc == NULL)
375 sc = pppoe_find_softc_by_hunique(p, len, rcvif);
376 break;
377 case PPPOE_TAG_ACCOOKIE:
378 if (ac_cookie == NULL) {
379 ac_cookie = p;
380 ac_cookie_len = len;
381 }
382 break;
383 case PPPOE_TAG_SNAME_ERR:
384 err_msg = "SERVICE NAME ERROR";
385 break;
386 case PPPOE_TAG_ACSYS_ERR:
387 err_msg = "AC SYSTEM ERROR";
388 break;
389 case PPPOE_TAG_GENERIC_ERR:
390 err_msg = "GENERIC ERROR";
391 break;
392 }
393 if (err_msg) {
394 printf("%s: %s\n", sc? sc->sc_sppp.pp_if.if_xname : "pppoe",
395 err_msg);
396 return;
397 }
398 if (size >= 0) {
399 size -= 4 + len;
400 if (len > 0)
401 p += len;
402 }
403 }
404 switch (code) {
405 case PPPOE_CODE_PADI:
406 case PPPOE_CODE_PADR:
407 /* ignore, we are no access concentrator */
408 return;
409 case PPPOE_CODE_PADO:
410 if (sc == NULL) {
411 printf("pppoe: received PADO but could not find request for it\n");
412 return;
413 }
414 if (sc->sc_state != PPPOE_STATE_PADI_SENT) {
415 printf("%s: received unexpected PADO\n", sc->sc_sppp.pp_if.if_xname);
416 return;
417 }
418 if (ac_cookie) {
419 sc->sc_ac_cookie = malloc(ac_cookie_len, M_DEVBUF, M_DONTWAIT);
420 if (sc->sc_ac_cookie == NULL)
421 return;
422 sc->sc_ac_cookie_len = ac_cookie_len;
423 memcpy(sc->sc_ac_cookie, ac_cookie, ac_cookie_len);
424 }
425 memcpy(&sc->sc_dest, eh->ether_shost, sizeof sc->sc_dest);
426 callout_stop(&sc->sc_timeout);
427 sc->sc_padr_retried = 0;
428 sc->sc_state = PPPOE_STATE_PADR_SENT;
429 if (pppoe_send_padr(sc) == 0)
430 callout_reset(&sc->sc_timeout, PPPOE_DISC_TIMEOUT*(1+sc->sc_padr_retried), pppoe_timeout, sc);
431 else
432 pppoe_abort_connect(sc);
433 break;
434 case PPPOE_CODE_PADS:
435 if (sc == NULL)
436 return;
437 sc->sc_session = session;
438 callout_stop(&sc->sc_timeout);
439 printf("%s: session 0x%x connected\n", sc->sc_sppp.pp_if.if_xname, session);
440 sc->sc_state = PPPOE_STATE_SESSION;
441 sc->sc_sppp.pp_up(&sc->sc_sppp); /* notify upper layers */
442 break;
443 case PPPOE_CODE_PADT:
444 if (sc == NULL)
445 return;
446 /* stop timer (we might be about to transmit a PADT ourself) */
447 callout_stop(&sc->sc_timeout);
448 /* signal upper layer */
449 printf("%s: session 0x%x terminated, received PADT\n", sc->sc_sppp.pp_if.if_xname, session);
450 /* clean up softc */
451 sc->sc_state = PPPOE_STATE_INITIAL;
452 memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest));
453 if (sc->sc_ac_cookie) {
454 free(sc->sc_ac_cookie, M_MBUF);
455 sc->sc_ac_cookie = NULL;
456 }
457 sc->sc_ac_cookie_len = 0;
458 sc->sc_session = 0;
459 sc->sc_sppp.pp_down(&sc->sc_sppp);
460 break;
461 default:
462 printf("%s: unknown code (0x%04x) session = 0x%04x\n",
463 sc? sc->sc_sppp.pp_if.if_xname : "pppoe",
464 code, session);
465 break;
466 }
467 }
468
469 static void
470 pppoe_disc_input(struct mbuf *m)
471 {
472 u_int8_t *p;
473 struct ether_header *eh;
474
475 eh = mtod(m, struct ether_header*);
476 m_adj(m, sizeof(struct ether_header));
477 p = mtod(m, u_int8_t*);
478 KASSERT(m->m_flags & M_PKTHDR);
479 pppoe_dispatch_disc_pkt(p, m->m_len, m->m_pkthdr.rcvif, eh);
480 m_free(m);
481 }
482
483 static void
484 pppoe_data_input(struct mbuf *m)
485 {
486 u_int8_t *p, vertype;
487 u_int16_t session, plen, code;
488 struct pppoe_softc *sc;
489
490 KASSERT(m->m_flags & M_PKTHDR);
491
492 m_adj(m, sizeof(struct ether_header));
493 if (m->m_pkthdr.len <= PPPOE_HEADERLEN) {
494 printf("pppoe (data): dropping too short packet: %ld bytes\n", (long)m->m_pkthdr.len);
495 goto drop;
496 }
497
498 p = mtod(m, u_int8_t*);
499
500 vertype = *p++;
501 if (vertype != PPPOE_VERTYPE) {
502 printf("pppoe (data): unknown version/type packet: 0x%x\n", vertype);
503 goto drop;
504 }
505
506 code = *p++;
507 if (code != 0)
508 goto drop;
509
510 PPPOE_READ_16(p, session);
511 sc = pppoe_find_softc_by_session(session, m->m_pkthdr.rcvif);
512 if (sc == NULL)
513 goto drop;
514
515 PPPOE_READ_16(p, plen);
516
517 #if NBPFILTER > 0
518 if(sc->sc_sppp.pp_if.if_bpf)
519 bpf_mtap(sc->sc_sppp.pp_if.if_bpf, m);
520 #endif
521
522 m_adj(m, PPPOE_HEADERLEN);
523
524 #ifdef PPPOE_DEBUG
525 {
526 struct mbuf *p;
527
528 printf("%s: pkthdr.len=%d, pppoe.len=%d",
529 sc->sc_sppp.pp_if.if_xname,
530 m->m_pkthdr.len, plen);
531 p = m;
532 while (p) {
533 printf(" l=%d", p->m_len);
534 p = p->m_next;
535 }
536 printf("\n");
537 }
538 #endif
539
540 if (m->m_pkthdr.len < plen)
541 goto drop;
542
543 /* fix incoming interface pointer (not the raw ethernet interface anymore) */
544 m->m_pkthdr.rcvif = &sc->sc_sppp.pp_if;
545
546 /* pass packet up */
547 sppp_input(&sc->sc_sppp.pp_if, m);
548 return;
549
550 drop:
551 m_free(m);
552 }
553
554 static int
555 pppoe_output(struct pppoe_softc *sc, struct mbuf *m)
556 {
557 struct sockaddr dst;
558 struct ether_header *eh;
559 u_int16_t etype;
560
561 if (sc->sc_eth_if == NULL)
562 return EIO;
563
564 memset(&dst, 0, sizeof dst);
565 dst.sa_family = AF_UNSPEC;
566 eh = (struct ether_header*)&dst.sa_data;
567 etype = sc->sc_state == PPPOE_STATE_SESSION? ETHERTYPE_PPPOE : ETHERTYPE_PPPOEDISC;
568 eh->ether_type = htons(etype);
569 memcpy(&eh->ether_dhost, &sc->sc_dest, sizeof sc->sc_dest);
570
571 #ifdef PPPOE_DEBUG
572 printf("%s (%x) state=%d, session=0x%x output -> %s, len=%d\n",
573 sc->sc_sppp.pp_if.if_xname, etype,
574 sc->sc_state, sc->sc_session,
575 ether_sprintf((const unsigned char *)&sc->sc_dest), m->m_pkthdr.len);
576 #endif
577
578 return sc->sc_eth_if->if_output(sc->sc_eth_if, m, &dst, NULL);
579 }
580
581 static int
582 pppoe_ioctl(struct ifnet *ifp, unsigned long cmd, caddr_t data)
583 {
584 struct proc *p = curproc; /* XXX */
585 struct pppoe_softc *sc = (struct pppoe_softc*)ifp;
586 int error = 0;
587
588 switch (cmd) {
589 case PPPOESETPARMS:
590 {
591 struct pppoediscparms *parms = (struct pppoediscparms*)data;
592 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
593 return error;
594 if (parms->eth_ifname[0] != 0) {
595 sc->sc_eth_if = ifunit(parms->eth_ifname);
596 if (sc->sc_eth_if == NULL)
597 return ENXIO;
598 }
599 if (parms->ac_name) {
600 size_t s;
601 char * p = malloc(parms->ac_name_len + 1, M_DEVBUF, M_WAITOK);
602 copyinstr(parms->ac_name, p, parms->ac_name_len, &s);
603 if (sc->sc_concentrator_name)
604 free(sc->sc_concentrator_name, M_DEVBUF);
605 sc->sc_concentrator_name = p;
606 }
607 if (parms->service_name) {
608 size_t s;
609 char * p = malloc(parms->service_name_len + 1, M_DEVBUF, M_WAITOK);
610 copyinstr(parms->service_name, p, parms->service_name_len, &s);
611 if (sc->sc_service_name)
612 free(sc->sc_service_name, M_DEVBUF);
613 sc->sc_service_name = p;
614 }
615 return 0;
616 }
617 break;
618 case PPPOEGETPARMS:
619 {
620 struct pppoediscparms *parms = (struct pppoediscparms*)data;
621 memset(parms, 0, sizeof *parms);
622 if (sc->sc_eth_if)
623 strncpy(parms->ifname, sc->sc_eth_if->if_xname, IFNAMSIZ);
624 return 0;
625 }
626 break;
627 default:
628 return sppp_ioctl(ifp, cmd, data);
629 }
630 return 0;
631 }
632
633 /*
634 * Allocate a mbuf/cluster with space to store the given data length
635 * of payload, leaving space for prepending an ethernet header
636 * in front.
637 */
638 static struct mbuf *
639 pppoe_get_mbuf(size_t len)
640 {
641 struct mbuf *m;
642
643 MGETHDR(m, M_DONTWAIT, MT_DATA);
644 if (m == NULL)
645 return NULL;
646 if (len+sizeof(struct ether_header) > MHLEN) {
647 MCLGET(m, M_DONTWAIT);
648 if ((m->m_flags & M_EXT) == 0) {
649 struct mbuf *n;
650 MFREE(m, n);
651 return 0;
652 }
653 }
654 m->m_data += sizeof(struct ether_header);
655 m->m_len = len;
656 m->m_pkthdr.len = len;
657 m->m_pkthdr.rcvif = NULL;
658
659 return m;
660 }
661
662 static int
663 pppoe_send_padi(struct pppoe_softc *sc)
664 {
665 struct mbuf *m0;
666 int len, l1, l2;
667 u_int8_t *p;
668
669 if (sc->sc_state >PPPOE_STATE_PADI_SENT)
670 panic("pppoe_send_padi in state %d", sc->sc_state);
671
672 /* calculate length of frame (excluding ethernet header + pppoe header) */
673 len = 2+2+2+2+sizeof sc; /* service name tag is required, host unique is send too */
674 if (sc->sc_service_name != NULL) {
675 l1 = strlen(sc->sc_service_name);
676 len += l1;
677 }
678 if (sc->sc_concentrator_name != NULL) {
679 l2 = strlen(sc->sc_concentrator_name);
680 len += 2+2+l2;
681 }
682
683 /* allocate a buffer */
684 m0 = pppoe_get_mbuf(len+PPPOE_HEADERLEN); /* header len + payload len */
685 if (!m0) return ENOBUFS;
686
687 /* fill in pkt */
688 p = mtod(m0, u_int8_t*);
689 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADI, 0, len);
690 PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
691 if (sc->sc_service_name != NULL) {
692 PPPOE_ADD_16(p, l1);
693 memcpy(p, sc->sc_service_name, l1);
694 p += l1;
695 } else {
696 PPPOE_ADD_16(p, 0);
697 }
698 if (sc->sc_concentrator_name != NULL) {
699 PPPOE_ADD_16(p, PPPOE_TAG_ACNAME);
700 PPPOE_ADD_16(p, l2);
701 memcpy(p, sc->sc_concentrator_name, l2);
702 p += l2;
703 }
704 PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
705 PPPOE_ADD_16(p, sizeof(sc));
706 memcpy(p, &sc, sizeof sc);
707
708 #ifdef PPPOE_DEBUG
709 p += sizeof sc;
710 if (p - mtod(m0, u_int8_t*) != len + PPPOE_HEADERLEN)
711 panic("pppoe_send_padi: garbled output len, should be %ld, is %ld",
712 (long)(len + PPPOE_HEADERLEN), (long)(p - mtod(m0, u_int8_t*)));
713 #endif
714
715 /* send pkt */
716 return pppoe_output(sc, m0);
717 }
718
719 static void
720 pppoe_timeout(void *arg)
721 {
722 int x;
723 struct pppoe_softc *sc = (struct pppoe_softc*)arg;
724
725 #ifdef PPPOE_DEBUG
726 printf("%s: timeout\n", sc->sc_sppp.pp_if.if_xname);
727 #endif
728
729 switch (sc->sc_state) {
730 case PPPOE_STATE_PADI_SENT:
731 x = splnet();
732 sc->sc_padi_retried++;
733 if (sc->sc_padi_retried >= PPPOE_DISC_MAXPADI) {
734 pppoe_abort_connect(sc);
735 splx(x);
736 return;
737 }
738 if (pppoe_send_padi(sc) == 0)
739 callout_reset(&sc->sc_timeout, PPPOE_DISC_TIMEOUT*(1+sc->sc_padi_retried), pppoe_timeout, sc);
740 else
741 pppoe_abort_connect(sc);
742 splx(x);
743 break;
744
745 case PPPOE_STATE_PADR_SENT:
746 x = splnet();
747 sc->sc_padr_retried++;
748 if (sc->sc_padr_retried >= PPPOE_DISC_MAXPADR) {
749 memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest));
750 sc->sc_state = PPPOE_STATE_PADI_SENT;
751 sc->sc_padr_retried = 0;
752 if (pppoe_send_padi(sc) == 0)
753 callout_reset(&sc->sc_timeout, PPPOE_DISC_TIMEOUT*(1+sc->sc_padi_retried), pppoe_timeout, sc);
754 else
755 pppoe_abort_connect(sc);
756 splx(x);
757 return;
758 }
759 if (pppoe_send_padr(sc) == 0)
760 callout_reset(&sc->sc_timeout, PPPOE_DISC_TIMEOUT*(1+sc->sc_padr_retried), pppoe_timeout, sc);
761 else
762 pppoe_abort_connect(sc);
763 splx(x);
764 break;
765 case PPPOE_STATE_CLOSING:
766 x = splnet();
767 pppoe_disconnect(sc);
768 splx(x);
769 break;
770 default:
771 return; /* all done, work in peace */
772 }
773 }
774
775 /* Start a connection (i.e. initiate discovery phase) */
776 static int
777 pppoe_connect(struct pppoe_softc *sc)
778 {
779 int x, err;
780
781 if (sc->sc_state != PPPOE_STATE_INITIAL)
782 return EBUSY;
783
784 x = splnet();
785 sc->sc_state = PPPOE_STATE_PADI_SENT;
786 sc->sc_padr_retried = 0;
787 err = pppoe_send_padi(sc);
788 if (err == 0)
789 callout_reset(&sc->sc_timeout, PPPOE_DISC_TIMEOUT, pppoe_timeout, sc);
790 else
791 pppoe_abort_connect(sc);
792 splx(x);
793 return err;
794 }
795
796 /* disconnect */
797 static int
798 pppoe_disconnect(struct pppoe_softc *sc)
799 {
800 int err, x;
801
802 x = splnet();
803
804 if (sc->sc_state < PPPOE_STATE_SESSION)
805 err = EBUSY;
806 else {
807 printf("%s: disconnecting\n", sc->sc_sppp.pp_if.if_xname);
808 err = pppoe_send_padt(sc);
809 }
810
811 /* cleanup softc */
812 sc->sc_state = PPPOE_STATE_INITIAL;
813 memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest));
814 if (sc->sc_ac_cookie) {
815 free(sc->sc_ac_cookie, M_MBUF);
816 sc->sc_ac_cookie = NULL;
817 }
818 sc->sc_ac_cookie_len = 0;
819 sc->sc_session = 0;
820
821 /* notify upper layer */
822 sc->sc_sppp.pp_down(&sc->sc_sppp);
823
824 splx(x);
825
826 return err;
827 }
828
829 /* Connection attempt aborted */
830 static void
831 pppoe_abort_connect(struct pppoe_softc *sc)
832 {
833 printf("%s: could not establish connection\n",
834 sc->sc_sppp.pp_if.if_xname);
835 sc->sc_state = PPPOE_STATE_INITIAL;
836 memcpy(&sc->sc_dest, etherbroadcastaddr, sizeof(sc->sc_dest));
837
838 /* notify upper layer */
839 sc->sc_sppp.pp_down(&sc->sc_sppp);
840 }
841
842 /* Send a PADR packet */
843 static int
844 pppoe_send_padr(struct pppoe_softc *sc)
845 {
846 struct mbuf *m0;
847 u_int8_t *p;
848 size_t len, l1;
849
850 if (sc->sc_state != PPPOE_STATE_PADR_SENT)
851 return EIO;
852
853 len = 2+2+2+2+sizeof(sc); /* service name, host unique */
854 if (sc->sc_service_name != NULL) { /* service name tag maybe empty */
855 l1 = strlen(sc->sc_service_name);
856 len += l1;
857 }
858 if (sc->sc_ac_cookie_len > 0)
859 len += 2+2+sc->sc_ac_cookie_len; /* AC cookie */
860 m0 = pppoe_get_mbuf(len+PPPOE_HEADERLEN);
861 if (!m0) return ENOBUFS;
862 p = mtod(m0, u_int8_t*);
863 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADR, 0, len);
864 PPPOE_ADD_16(p, PPPOE_TAG_SNAME);
865 if (sc->sc_service_name != NULL) {
866 PPPOE_ADD_16(p, l1);
867 memcpy(p, sc->sc_service_name, l1);
868 p += l1;
869 } else {
870 PPPOE_ADD_16(p, 0);
871 }
872 if (sc->sc_ac_cookie_len > 0) {
873 PPPOE_ADD_16(p, PPPOE_TAG_ACCOOKIE);
874 PPPOE_ADD_16(p, sc->sc_ac_cookie_len);
875 memcpy(p, sc->sc_ac_cookie, sc->sc_ac_cookie_len);
876 p += sc->sc_ac_cookie_len;
877 }
878 PPPOE_ADD_16(p, PPPOE_TAG_HUNIQUE);
879 PPPOE_ADD_16(p, sizeof(sc));
880 memcpy(p, &sc, sizeof sc);
881
882 #ifdef PPPOE_DEBUG
883 p += sizeof sc;
884 if (p - mtod(m0, u_int8_t*) != len + PPPOE_HEADERLEN)
885 panic("pppoe_send_padr: garbled output len, should be %ld, is %ld",
886 (long)(len + PPPOE_HEADERLEN), (long)(p - mtod(m0, u_int8_t*)));
887 #endif
888
889 return pppoe_output(sc, m0);
890 }
891
892 /* send a PADT packet */
893 static int
894 pppoe_send_padt(struct pppoe_softc *sc)
895 {
896 struct mbuf *m0;
897 u_int8_t *p;
898
899 if (sc->sc_state < PPPOE_STATE_SESSION)
900 return EIO;
901
902 #ifdef PPPOE_DEBUG
903 printf("%s: sending PADT\n", sc->sc_sppp.pp_if.if_xname);
904 #endif
905 m0 = pppoe_get_mbuf(PPPOE_HEADERLEN);
906 if (!m0) return ENOBUFS;
907 p = mtod(m0, u_int8_t*);
908 PPPOE_ADD_HEADER(p, PPPOE_CODE_PADT, sc->sc_session, 0);
909 return pppoe_output(sc, m0);
910 }
911
912 static void
913 pppoe_tls(struct sppp *sp)
914 {
915 struct pppoe_softc *sc = (void*)sp;
916 if (sc->sc_state != PPPOE_STATE_INITIAL)
917 return;
918 pppoe_connect(sc);
919 }
920
921 static void
922 pppoe_tlf(struct sppp *sp)
923 {
924 struct pppoe_softc *sc = (void*)sp;
925 if (sc->sc_state < PPPOE_STATE_SESSION)
926 return;
927 /*
928 * Do not call pppoe_disconnect here, the upper layer state
929 * machine gets confused by this. We must return from this
930 * function and defer disconnecting to the timeout handler.
931 */
932 sc->sc_state = PPPOE_STATE_CLOSING;
933 callout_reset(&sc->sc_timeout, hz/100, pppoe_timeout, sc);
934 }
935
936 static void
937 pppoe_start(struct ifnet *ifp)
938 {
939 struct pppoe_softc *sc = (void*)ifp;
940 struct mbuf *m;
941 u_int8_t *p;
942 size_t len;
943
944 if (sppp_isempty(ifp))
945 return;
946
947 /* are we read to proccess data yet? */
948 if (sc->sc_state < PPPOE_STATE_SESSION) {
949 sppp_flush(&sc->sc_sppp.pp_if);
950 return;
951 }
952
953 while ((m = sppp_dequeue(ifp)) != NULL) {
954 len = m->m_pkthdr.len;
955 M_PREPEND(m, PPPOE_HEADERLEN, M_DONTWAIT);
956 if (m == NULL) {
957 m_free(m);
958 break;
959 }
960 p = mtod(m, u_int8_t*);
961 PPPOE_ADD_HEADER(p, 0, sc->sc_session, len);
962
963 #if NBPFILTER > 0
964 if(sc->sc_sppp.pp_if.if_bpf)
965 bpf_mtap(sc->sc_sppp.pp_if.if_bpf, m);
966 #endif
967
968 pppoe_output(sc, m);
969 }
970 }
971
972 #endif /* NPPPOE > 0 */
973