diff options
author | François Cachereul <f.cachereul@alphalink.fr> | 2015-01-27 12:07:01 +0100 |
---|---|---|
committer | Dmitry Kozlov <xeb@mail.ru> | 2015-02-20 11:21:41 +0300 |
commit | e4b4e0eef7fe04f7c202177cee82aa5ed4829387 (patch) | |
tree | 50c1f9e8bfd325e161f9d6ab89c38e419cec3bf2 /accel-pppd | |
parent | 44d70d146370ba2bb88ab0c8d2fad60deca696ab (diff) | |
download | accel-ppp-e4b4e0eef7fe04f7c202177cee82aa5ed4829387.tar.gz accel-ppp-e4b4e0eef7fe04f7c202177cee82aa5ed4829387.zip |
ppp: create ppp units after authentication
This avaid allocating a ppp unit when authentication failed
Split establish_ppp in two functions estabish_ppp and
connect_ppp_channel. The fist one connect the channel on an instance of
/dev/ppp, allocate channel resources and start first ppp layer.
The second functions create ppp unit and connect the channel to this
unit. It is called after authentication.
destablish_ppp is also split in two function for symmetry and
ppp_terminate is adapted to handle the case when the unit is not
created.
Signed-off-by: François Cachereul <f.cachereul@alphalink.fr>
Diffstat (limited to 'accel-pppd')
-rw-r--r-- | accel-pppd/ppp/ppp.c | 110 | ||||
-rw-r--r-- | accel-pppd/ppp/ppp.h | 1 | ||||
-rw-r--r-- | accel-pppd/ppp/ppp_auth.c | 4 |
3 files changed, 70 insertions, 45 deletions
diff --git a/accel-pppd/ppp/ppp.c b/accel-pppd/ppp/ppp.c index 2bc0ab38..6c80b9ca 100644 --- a/accel-pppd/ppp/ppp.c +++ b/accel-pppd/ppp/ppp.c @@ -76,9 +76,6 @@ void __export ppp_init(struct ppp_t *ppp) int __export establish_ppp(struct ppp_t *ppp) { - struct pppunit_cache *uc = NULL; - struct ifreq ifr; - if (ap_shutdown) return -1; @@ -95,12 +92,54 @@ int __export establish_ppp(struct ppp_t *ppp) } fcntl(ppp->chan_fd, F_SETFD, fcntl(ppp->chan_fd, F_GETFD) | FD_CLOEXEC); + if (fcntl(ppp->chan_fd, F_SETFL, O_NONBLOCK)) { + log_ppp_error("ppp: cannot set nonblocking mode: %s\n", + strerror(errno)); + goto exit_close_chan; + } if (ioctl(ppp->chan_fd, PPPIOCATTCHAN, &ppp->chan_idx) < 0) { log_ppp_error("ioctl(PPPIOCATTCHAN): %s\n", strerror(errno)); goto exit_close_chan; } + init_layers(ppp); + if (list_empty(&ppp->layers)) { + log_ppp_error("no layers to start\n"); + goto exit_close_chan; + } + + ppp->buf = mempool_alloc(buf_pool); + + ppp->chan_hnd.fd = ppp->chan_fd; + ppp->chan_hnd.read = ppp_chan_read; + + log_ppp_debug("ppp establishing\n"); + + if (ap_session_starting(&ppp->ses)) + goto exit_free_buf; + + triton_md_register_handler(ppp->ses.ctrl->ctx, &ppp->chan_hnd); + triton_md_enable_handler(&ppp->chan_hnd, MD_MODE_READ); + + start_first_layer(ppp); + + return 0; + +exit_free_buf: + mempool_free(ppp->buf); + ppp->buf = NULL; +exit_close_chan: + close(ppp->chan_fd); + + return -1; +} + +int __export connect_ppp_channel(struct ppp_t *ppp) +{ + struct pppunit_cache *uc = NULL; + struct ifreq ifr; + if (uc_size) { pthread_mutex_lock(&uc_lock); if (!list_empty(&uc_list)) { @@ -119,7 +158,7 @@ int __export establish_ppp(struct ppp_t *ppp) ppp->unit_fd = open("/dev/ppp", O_RDWR); if (ppp->unit_fd < 0) { log_ppp_error("open(unit) /dev/ppp: %s\n", strerror(errno)); - goto exit_close_chan; + goto exit; } fcntl(ppp->unit_fd, F_SETFD, fcntl(ppp->unit_fd, F_GETFD) | FD_CLOEXEC); @@ -141,11 +180,6 @@ int __export establish_ppp(struct ppp_t *ppp) goto exit_close_unit; } - if (fcntl(ppp->chan_fd, F_SETFL, O_NONBLOCK)) { - log_ppp_error("ppp: cannot set nonblocking mode: %s\n", strerror(errno)); - goto exit_close_unit; - } - sprintf(ppp->ses.ifname, "ppp%i", ppp->ses.unit_idx); log_ppp_info1("connect: %s <--> %s(%s)\n", ppp->ses.ifname, ppp->ses.ctrl->name, ppp->ses.chan_name); @@ -161,53 +195,45 @@ int __export establish_ppp(struct ppp_t *ppp) goto exit_close_unit; } - init_layers(ppp); - - if (list_empty(&ppp->layers)) { - log_ppp_error("no layers to start\n"); - goto exit_close_unit; - } - - ppp->buf = mempool_alloc(buf_pool); - - ppp->chan_hnd.fd = ppp->chan_fd; - ppp->chan_hnd.read = ppp_chan_read; ppp->unit_hnd.fd = ppp->unit_fd; ppp->unit_hnd.read = ppp_unit_read; - log_ppp_debug("ppp established\n"); - - if (ap_session_starting(&ppp->ses)) - goto exit_free_buf; + log_ppp_debug("ppp connected\n"); - triton_md_register_handler(ppp->ses.ctrl->ctx, &ppp->chan_hnd); triton_md_register_handler(ppp->ses.ctrl->ctx, &ppp->unit_hnd); - - triton_md_enable_handler(&ppp->chan_hnd, MD_MODE_READ); triton_md_enable_handler(&ppp->unit_hnd, MD_MODE_READ); - start_first_layer(ppp); - return 0; -exit_free_buf: - mempool_free(ppp->buf); - ppp->buf = NULL; exit_close_unit: close(ppp->unit_fd); -exit_close_chan: - close(ppp->chan_fd); - + ppp->unit_fd = -1; +exit: return -1; } +static void destroy_ppp_channel(struct ppp_t *ppp) +{ + triton_md_unregister_handler(&ppp->chan_hnd, 1); + close(ppp->fd); + ppp->fd = -1; + ppp->chan_fd = -1; + + _free_layers(ppp); + + mempool_free(ppp->buf); + + ap_session_finished(&ppp->ses); +} + static void destablish_ppp(struct ppp_t *ppp) { struct pppunit_cache *uc; - triton_event_fire(EV_SES_PRE_FINISHED, &ppp->ses); + if (ppp->unit_fd < 0) + goto destroy_channel; - triton_md_unregister_handler(&ppp->chan_hnd, 1); + triton_event_fire(EV_SES_PRE_FINISHED, &ppp->ses); if (conf_unit_cache) { struct ifreq ifr; @@ -235,18 +261,12 @@ static void destablish_ppp(struct ppp_t *ppp) triton_md_unregister_handler(&ppp->unit_hnd, 1); skip: - close(ppp->fd); - ppp->fd = -1; - ppp->chan_fd = -1; ppp->unit_fd = -1; - _free_layers(ppp); - log_ppp_debug("ppp destablished\n"); - mempool_free(ppp->buf); - - ap_session_finished(&ppp->ses); +destroy_channel: + destroy_ppp_channel(ppp); } static void *uc_thread(void *unused) diff --git a/accel-pppd/ppp/ppp.h b/accel-pppd/ppp/ppp.h index 759e114a..dfceaccd 100644 --- a/accel-pppd/ppp/ppp.h +++ b/accel-pppd/ppp/ppp.h @@ -96,6 +96,7 @@ struct ppp_handler_t void ppp_init(struct ppp_t *ppp); int establish_ppp(struct ppp_t *ppp); +int connect_ppp_channel(struct ppp_t *ppp); int ppp_chan_send(struct ppp_t *ppp, void *data, int size); int ppp_unit_send(struct ppp_t *ppp, void *data, int size); void lcp_send_proto_rej(struct ppp_t *ppp, uint16_t proto); diff --git a/accel-pppd/ppp/ppp_auth.c b/accel-pppd/ppp/ppp_auth.c index a387ba2d..238b21ea 100644 --- a/accel-pppd/ppp/ppp_auth.c +++ b/accel-pppd/ppp/ppp_auth.c @@ -329,6 +329,7 @@ static void __ppp_auth_started(struct ppp_t *ppp) log_ppp_info1("%s: authentication succeeded\n", ppp->ses.username); + triton_event_fire(EV_SES_AUTHORIZED, &ppp->ses); } @@ -341,6 +342,9 @@ int __export ppp_auth_succeeded(struct ppp_t *ppp, char *username) return -1; } + if (connect_ppp_channel(ppp)) + return -1; + triton_context_call(ppp->ses.ctrl->ctx, (triton_event_func)__ppp_auth_started, ppp); return 0; |