3 * PPP library with GLib integration
5 * Copyright (C) 2009-2011 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 #include <arpa/inet.h>
40 #include "crc-ccitt.h"
43 #define DEFAULT_MRU 1500
44 #define DEFAULT_MTU 1500
46 #define PPP_ADDR_FIELD 0xff
49 #define GUARD_TIMEOUTS 1500
52 PPP_PHASE_DEAD = 0, /* Link dead */
53 PPP_PHASE_ESTABLISHMENT, /* LCP started */
54 PPP_PHASE_AUTHENTICATION, /* Auth started */
55 PPP_PHASE_NETWORK, /* IPCP started */
56 PPP_PHASE_LINK_UP, /* IPCP negotiation ok, link up */
57 PPP_PHASE_TERMINATION, /* LCP Terminate phase */
63 struct pppcp_data *lcp;
64 struct pppcp_data *ipcp;
66 struct ppp_chap *chap;
72 GAtPPPConnectFunc connect_cb;
73 gpointer connect_data;
74 GAtPPPDisconnectFunc disconnect_cb;
75 gpointer disconnect_data;
76 GAtPPPDisconnectReason disconnect_reason;
80 guint ppp_dead_source;
81 GAtSuspendFunc suspend_func;
82 gpointer suspend_data;
84 guint guard_timeout_source;
90 void ppp_debug(GAtPPP *ppp, const char *str)
92 if (ppp == NULL || ppp->debugf == NULL)
95 ppp->debugf(str, ppp->debug_data);
98 static gboolean ppp_dead(gpointer userdata)
100 GAtPPP *ppp = userdata;
104 ppp->ppp_dead_source = 0;
106 /* notify interested parties */
107 if (ppp->disconnect_cb)
108 ppp->disconnect_cb(ppp->disconnect_reason,
109 ppp->disconnect_data);
114 static void sta_sent(gpointer userdata)
116 GAtPPP *ppp = userdata;
120 ppp->sta_pending = FALSE;
122 if (ppp->phase == PPP_PHASE_DEAD)
126 struct ppp_header *ppp_packet_new(gsize infolen, guint16 protocol)
128 struct ppp_header *ppp_packet;
130 ppp_packet = g_try_malloc0(infolen + sizeof(*ppp_packet));
131 if (ppp_packet == NULL)
134 ppp_packet->proto = htons(protocol);
135 ppp_packet->address = PPP_ADDR_FIELD;
136 ppp_packet->control = PPP_CTRL;
142 * Silently discard packets which are received when they shouldn't be
144 static inline gboolean ppp_drop_packet(GAtPPP *ppp, guint16 protocol)
146 switch (ppp->phase) {
147 case PPP_PHASE_ESTABLISHMENT:
148 case PPP_PHASE_TERMINATION:
149 if (protocol != LCP_PROTOCOL)
152 case PPP_PHASE_AUTHENTICATION:
153 if (protocol != LCP_PROTOCOL && protocol != CHAP_PROTOCOL)
158 case PPP_PHASE_NETWORK:
159 if (protocol != LCP_PROTOCOL && protocol != CHAP_PROTOCOL &&
160 protocol != IPCP_PROTO)
163 case PPP_PHASE_LINK_UP:
170 static void ppp_receive(const unsigned char *buf, gsize len, void *data)
173 unsigned int offset = 0;
175 const guint8 *packet;
180 if (buf[0] == PPP_ADDR_FIELD && len >= 2 && buf[1] == PPP_CTRL)
183 if (len < offset + 1)
187 * the Protocol field uses an extension mechanism consistent with the
188 * ISO 3309 extension mechanism for the Address field; the Least
189 * Significant Bit (LSB) of each octet is used to indicate extension
190 * of the Protocol field. A binary "0" as the LSB indicates that the
191 * Protocol field continues with the following octet. The presence
192 * of a binary "1" as the LSB marks the last octet of the Protocol
195 * To check for compression we simply check the LSB of the first
199 if (buf[offset] & 0x1) {
200 protocol = buf[offset];
203 if (len < offset + 2)
206 protocol = get_host_short(buf + offset);
210 if (ppp_drop_packet(ppp, protocol))
213 packet = buf + offset;
217 ppp_net_process_packet(ppp->net, packet, len - offset);
220 pppcp_process_packet(ppp->lcp, packet, len - offset);
223 pppcp_process_packet(ppp->ipcp, packet, len - offset);
227 ppp_chap_process_packet(ppp->chap, packet,
233 pppcp_send_protocol_reject(ppp->lcp, buf, len);
238 static void ppp_send_lcp_frame(GAtPPP *ppp, guint8 *packet, guint infolen)
240 struct ppp_header *header = (struct ppp_header *) packet;
242 guint32 xmit_accm = 0;
243 gboolean sta = FALSE;
247 * all LCP Link Configuration, Link Termination, and Code-Reject
248 * packets must be sent with the default sending ACCM
250 code = pppcp_get_code(packet);
251 lcp = code > 0 && code < 8;
254 * If we're going down, we try to make sure to send the final
255 * ack before informing the upper layers via the ppp_disconnect
256 * function. Once we enter PPP_DEAD phase, no further packets
259 if (code == PPPCP_CODE_TYPE_TERMINATE_ACK)
263 xmit_accm = g_at_hdlc_get_xmit_accm(ppp->hdlc);
264 g_at_hdlc_set_xmit_accm(ppp->hdlc, ~0U);
267 header->address = PPP_ADDR_FIELD;
268 header->control = PPP_CTRL;
270 if (g_at_hdlc_send(ppp->hdlc, packet,
271 infolen + sizeof(*header)) == TRUE) {
273 GAtIO *io = g_at_hdlc_get_io(ppp->hdlc);
275 ppp->sta_pending = TRUE;
276 g_at_io_set_write_done(io, sta_sent, ppp);
279 DBG(ppp, "Failed to send a frame\n");
282 g_at_hdlc_set_xmit_accm(ppp->hdlc, xmit_accm);
285 static void ppp_send_acfc_frame(GAtPPP *ppp, guint8 *packet,
288 struct ppp_header *header = (struct ppp_header *) packet;
294 /* We remove the only address and control field */
295 if (g_at_hdlc_send(ppp->hdlc, packet + offset,
296 infolen + sizeof(*header) - offset)
298 DBG(ppp, "Failed to send a frame\n");
301 static void ppp_send_acfc_pfc_frame(GAtPPP *ppp, guint8 *packet,
304 struct ppp_header *header = (struct ppp_header *) packet;
307 if (ppp->xmit_acfc && ppp->xmit_pfc)
309 else if (ppp->xmit_acfc)
311 else if (ppp->xmit_pfc) {
312 /* Shuffle AC bytes in place of the first protocol byte */
313 packet[2] = packet[1];
314 packet[1] = packet[0];
318 if (g_at_hdlc_send(ppp->hdlc, packet + offset,
319 infolen + sizeof(*header) - offset)
321 DBG(ppp, "Failed to send a frame\n");
325 * transmit out through the lower layer interface
327 * infolen - length of the information part of the packet
329 void ppp_transmit(GAtPPP *ppp, guint8 *packet, guint infolen)
331 guint16 proto = ppp_proto(packet);
333 if (proto == LCP_PROTOCOL) {
334 ppp_send_lcp_frame(ppp, packet, infolen);
339 * If the upper 8 bits of the protocol are 0, then send
340 * with PFC if enabled
342 if ((proto & 0xff00) == 0)
343 ppp_send_acfc_pfc_frame(ppp, packet, infolen);
345 ppp_send_acfc_frame(ppp, packet, infolen);
348 static inline void ppp_enter_phase(GAtPPP *ppp, enum ppp_phase phase)
350 DBG(ppp, "%d", phase);
353 if (phase == PPP_PHASE_DEAD && ppp->sta_pending == FALSE)
354 ppp->ppp_dead_source = g_idle_add(ppp_dead, ppp);
357 void ppp_set_auth(GAtPPP *ppp, const guint8* auth_data)
359 guint16 proto = get_host_short(auth_data);
364 ppp_chap_free(ppp->chap);
366 ppp->chap = ppp_chap_new(ppp, auth_data[2]);
369 DBG(ppp, "unknown authentication proto");
374 void ppp_auth_notify(GAtPPP *ppp, gboolean success)
376 if (success == FALSE) {
377 ppp->disconnect_reason = G_AT_PPP_REASON_AUTH_FAIL;
378 pppcp_signal_close(ppp->lcp);
382 ppp_enter_phase(ppp, PPP_PHASE_NETWORK);
384 /* Send UP & OPEN events to the IPCP layer */
385 pppcp_signal_open(ppp->ipcp);
386 pppcp_signal_up(ppp->ipcp);
389 void ppp_ipcp_up_notify(GAtPPP *ppp, const char *local, const char *peer,
390 const char *dns1, const char *dns2)
392 ppp->net = ppp_net_new(ppp, ppp->fd);
395 * ppp_net_new took control over the fd, whatever happens is out of
400 if (ppp->net == NULL) {
401 ppp->disconnect_reason = G_AT_PPP_REASON_NET_FAIL;
402 pppcp_signal_close(ppp->lcp);
406 if (ppp_net_set_mtu(ppp->net, ppp->mtu) == FALSE)
407 DBG(ppp, "Unable to set MTU");
409 ppp_enter_phase(ppp, PPP_PHASE_LINK_UP);
412 ppp->connect_cb(ppp_net_get_interface(ppp->net),
413 local, peer, dns1, dns2,
417 void ppp_ipcp_down_notify(GAtPPP *ppp)
419 /* Most likely we failed to create the interface */
420 if (ppp->net == NULL)
423 ppp_net_free(ppp->net);
427 void ppp_ipcp_finished_notify(GAtPPP *ppp)
429 if (ppp->phase != PPP_PHASE_NETWORK)
432 /* Our IPCP parameter negotiation failed */
433 ppp->disconnect_reason = G_AT_PPP_REASON_IPCP_FAIL;
434 pppcp_signal_close(ppp->ipcp);
435 pppcp_signal_close(ppp->lcp);
438 void ppp_lcp_up_notify(GAtPPP *ppp)
440 /* Wait for the peer to send us a challenge if we expect auth */
441 if (ppp->chap != NULL) {
442 ppp_enter_phase(ppp, PPP_PHASE_AUTHENTICATION);
446 /* Otherwise proceed as if auth succeeded */
447 ppp_auth_notify(ppp, TRUE);
450 void ppp_lcp_down_notify(GAtPPP *ppp)
452 if (ppp->phase == PPP_PHASE_NETWORK || ppp->phase == PPP_PHASE_LINK_UP)
453 pppcp_signal_down(ppp->ipcp);
455 if (ppp->disconnect_reason == G_AT_PPP_REASON_UNKNOWN)
456 ppp->disconnect_reason = G_AT_PPP_REASON_PEER_CLOSED;
458 ppp_enter_phase(ppp, PPP_PHASE_TERMINATION);
461 void ppp_lcp_finished_notify(GAtPPP *ppp)
463 ppp_enter_phase(ppp, PPP_PHASE_DEAD);
466 void ppp_set_recv_accm(GAtPPP *ppp, guint32 accm)
468 g_at_hdlc_set_recv_accm(ppp->hdlc, accm);
471 void ppp_set_xmit_accm(GAtPPP *ppp, guint32 accm)
473 g_at_hdlc_set_xmit_accm(ppp->hdlc, accm);
477 * The only time we use other than default MTU is when we are in
480 void ppp_set_mtu(GAtPPP *ppp, const guint8 *data)
482 guint16 mtu = get_host_short(data);
487 void ppp_set_xmit_acfc(GAtPPP *ppp, gboolean acfc)
489 ppp->xmit_acfc = acfc;
492 void ppp_set_xmit_pfc(GAtPPP *ppp, gboolean pfc)
497 static void io_disconnect(gpointer user_data)
499 GAtPPP *ppp = user_data;
501 if (ppp->phase == PPP_PHASE_DEAD)
504 ppp->disconnect_reason = G_AT_PPP_REASON_LINK_DEAD;
505 pppcp_signal_down(ppp->lcp);
506 pppcp_signal_close(ppp->lcp);
509 static void ppp_proxy_suspend_net_interface(gpointer user_data)
511 GAtPPP *ppp = user_data;
513 ppp->suspended = TRUE;
514 ppp_net_suspend_interface(ppp->net);
516 if (ppp->suspend_func)
517 ppp->suspend_func(ppp->suspend_data);
520 gboolean g_at_ppp_listen(GAtPPP *ppp, GAtIO *io)
522 ppp->hdlc = g_at_hdlc_new_from_io(io);
523 if (ppp->hdlc == NULL)
526 ppp->suspended = FALSE;
527 g_at_hdlc_set_receive(ppp->hdlc, ppp_receive, ppp);
528 g_at_hdlc_set_suspend_function(ppp->hdlc,
529 ppp_proxy_suspend_net_interface, ppp);
530 g_at_io_set_disconnect_function(io, io_disconnect, ppp);
532 ppp_enter_phase(ppp, PPP_PHASE_ESTABLISHMENT);
537 /* Administrative Open */
538 gboolean g_at_ppp_open(GAtPPP *ppp, GAtIO *io)
540 ppp->hdlc = g_at_hdlc_new_from_io(io);
541 if (ppp->hdlc == NULL)
544 ppp->suspended = FALSE;
545 g_at_hdlc_set_receive(ppp->hdlc, ppp_receive, ppp);
546 g_at_hdlc_set_suspend_function(ppp->hdlc,
547 ppp_proxy_suspend_net_interface, ppp);
548 g_at_hdlc_set_no_carrier_detect(ppp->hdlc, TRUE);
549 g_at_io_set_disconnect_function(io, io_disconnect, ppp);
551 /* send an UP & OPEN events to the lcp layer */
552 pppcp_signal_up(ppp->lcp);
553 pppcp_signal_open(ppp->lcp);
555 ppp_enter_phase(ppp, PPP_PHASE_ESTABLISHMENT);
560 gboolean g_at_ppp_set_credentials(GAtPPP *ppp, const char *username,
561 const char *password)
563 if (username && strlen(username) > 255)
566 if (password && strlen(password) > 255)
569 memset(ppp->username, 0, sizeof(ppp->username));
570 memset(ppp->password, 0, sizeof(ppp->password));
573 strcpy(ppp->username, username);
576 strcpy(ppp->password, password);
581 const char *g_at_ppp_get_username(GAtPPP *ppp)
583 return ppp->username;
586 const char *g_at_ppp_get_password(GAtPPP *ppp)
588 return ppp->password;
591 void g_at_ppp_set_recording(GAtPPP *ppp, const char *filename)
596 g_at_hdlc_set_recording(ppp->hdlc, filename);
599 void g_at_ppp_set_connect_function(GAtPPP *ppp, GAtPPPConnectFunc func,
605 ppp->connect_cb = func;
606 ppp->connect_data = user_data;
609 void g_at_ppp_set_disconnect_function(GAtPPP *ppp, GAtPPPDisconnectFunc func,
615 ppp->disconnect_cb = func;
616 ppp->disconnect_data = user_data;
619 void g_at_ppp_set_debug(GAtPPP *ppp, GAtDebugFunc func, gpointer user_data)
625 ppp->debug_data = user_data;
628 void g_at_ppp_set_suspend_function(GAtPPP *ppp, GAtSuspendFunc func,
634 ppp->suspend_func = func;
635 ppp->suspend_data = user_data;
637 if (ppp->hdlc != NULL)
638 g_at_hdlc_set_suspend_function(ppp->hdlc,
639 ppp_proxy_suspend_net_interface, ppp);
642 void g_at_ppp_shutdown(GAtPPP *ppp)
644 if (ppp->phase == PPP_PHASE_DEAD || ppp->phase == PPP_PHASE_TERMINATION)
647 ppp->disconnect_reason = G_AT_PPP_REASON_LOCAL_CLOSE;
648 pppcp_signal_close(ppp->lcp);
651 static gboolean call_suspend_cb(gpointer user_data)
653 GAtPPP *ppp = user_data;
655 ppp->guard_timeout_source = 0;
657 if (ppp->suspend_func)
658 ppp->suspend_func(ppp->suspend_data);
663 static gboolean send_escape_sequence(gpointer user_data)
665 GAtPPP *ppp = user_data;
666 GAtIO *io = g_at_hdlc_get_io(ppp->hdlc);
668 g_at_io_write(io, "+++", 3);
669 ppp->guard_timeout_source = g_timeout_add(GUARD_TIMEOUTS,
670 call_suspend_cb, ppp);
675 void g_at_ppp_suspend(GAtPPP *ppp)
680 ppp->suspended = TRUE;
681 ppp_net_suspend_interface(ppp->net);
682 g_at_hdlc_suspend(ppp->hdlc);
683 ppp->guard_timeout_source = g_timeout_add(GUARD_TIMEOUTS,
684 send_escape_sequence, ppp);
687 void g_at_ppp_resume(GAtPPP *ppp)
692 if (g_at_hdlc_get_io(ppp->hdlc) == NULL) {
697 ppp->suspended = FALSE;
698 g_at_io_set_disconnect_function(g_at_hdlc_get_io(ppp->hdlc),
700 ppp_net_resume_interface(ppp->net);
701 g_at_hdlc_resume(ppp->hdlc);
704 void g_at_ppp_ref(GAtPPP *ppp)
706 g_atomic_int_inc(&ppp->ref_count);
709 void g_at_ppp_unref(GAtPPP *ppp)
716 is_zero = g_atomic_int_dec_and_test(&ppp->ref_count);
718 if (is_zero == FALSE)
721 if (ppp->suspended == FALSE)
722 g_at_io_set_disconnect_function(g_at_hdlc_get_io(ppp->hdlc),
726 ppp_net_free(ppp->net);
727 else if (ppp->fd >= 0)
731 ppp_chap_free(ppp->chap);
734 ipcp_free(ppp->ipcp);
736 if (ppp->ppp_dead_source) {
737 g_source_remove(ppp->ppp_dead_source);
738 ppp->ppp_dead_source = 0;
741 if (ppp->guard_timeout_source) {
742 g_source_remove(ppp->guard_timeout_source);
743 ppp->guard_timeout_source = 0;
746 g_at_hdlc_unref(ppp->hdlc);
751 void g_at_ppp_set_server_info(GAtPPP *ppp, const char *remote,
752 const char *dns1, const char *dns2)
758 inet_pton(AF_INET, remote, &r);
759 inet_pton(AF_INET, dns1, &d1);
760 inet_pton(AF_INET, dns2, &d2);
762 ipcp_set_server_info(ppp->ipcp, r, d1, d2);
765 void g_at_ppp_set_acfc_enabled(GAtPPP *ppp, gboolean enabled)
767 lcp_set_acfc_enabled(ppp->lcp, enabled);
770 void g_at_ppp_set_pfc_enabled(GAtPPP *ppp, gboolean enabled)
772 lcp_set_pfc_enabled(ppp->lcp, enabled);
775 static GAtPPP *ppp_init_common(gboolean is_server, guint32 ip)
779 ppp = g_try_malloc0(sizeof(GAtPPP));
784 ppp->suspended = TRUE;
787 /* set options to defaults */
788 ppp->mru = DEFAULT_MRU;
789 ppp->mtu = DEFAULT_MTU;
791 /* initialize the lcp state */
792 ppp->lcp = lcp_new(ppp, is_server);
794 /* initialize IPCP state */
795 ppp->ipcp = ipcp_new(ppp, is_server, ip);
800 GAtPPP *g_at_ppp_new(void)
802 return ppp_init_common(FALSE, 0);
805 GAtPPP *g_at_ppp_server_new_full(const char *local, int fd)
812 else if (inet_pton(AF_INET, local, &ip) != 1)
815 ppp = ppp_init_common(TRUE, ip);
823 GAtPPP *g_at_ppp_server_new(const char *local)
825 return g_at_ppp_server_new_full(local, -1);