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