]> git.ozlabs.org Git - ppp.git/blobdiff - pppd/main.c
Added log messages for LCP Identification, Time-Remaining, and other
[ppp.git] / pppd / main.c
index 25076ca50d3aa59bfdc4a8b26264aeb8a764dd4c..f136a281291b297e8b81ace181b57397f0764d2a 100644 (file)
@@ -66,7 +66,7 @@
  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#define RCSID  "$Id: main.c,v 1.143 2004/11/06 05:42:29 paulus Exp $"
+#define RCSID  "$Id: main.c,v 1.149 2004/11/14 22:53:42 carlsonj Exp $"
 
 #include <stdio.h>
 #include <ctype.h>
@@ -165,13 +165,14 @@ void (*snoop_recv_hook) __P((unsigned char *p, int len)) = NULL;
 void (*snoop_send_hook) __P((unsigned char *p, int len)) = NULL;
 
 static int conn_running;       /* we have a [dis]connector running */
-static int devfd;              /* fd of underlying device */
-static int fd_ppp = -1;                /* fd for talking PPP */
 static int fd_loop;            /* fd for getting demand-dial packets */
-static int fd_devnull;         /* fd for /dev/null */
 
+int fd_devnull;                        /* fd for /dev/null */
+int devfd = -1;                        /* fd of underlying device */
+int fd_ppp = -1;               /* fd for talking PPP */
 int phase;                     /* where the link is at */
 int kill_link;
+int asked_to_quit;
 int open_ccp_flag;
 int listen_time;
 int got_sigusr2;
@@ -207,6 +208,9 @@ int link_stats_valid;
 
 int error_count;
 
+bool bundle_eof;
+bool bundle_terminating;
+
 /*
  * We maintain a list of child process pids and
  * functions to call when they exit.
@@ -413,11 +417,6 @@ main(argc, argv)
     if (dryrun)
        die(0);
 
-    /*
-     * Initialize system-dependent stuff.
-     */
-    sys_init();
-
     /* Make sure fds 0, 1, 2 are open to somewhere. */
     fd_devnull = open(_PATH_DEVNULL, O_RDWR);
     if (fd_devnull < 0)
@@ -429,6 +428,11 @@ main(argc, argv)
        fd_devnull = i;
     }
 
+    /*
+     * Initialize system-dependent stuff.
+     */
+    sys_init();
+
 #ifdef USE_TDB
     pppdb = tdb_open(_PATH_PPPDB, 0, 0, O_RDWR|O_CREAT, 0644);
     if (pppdb != NULL) {
@@ -467,6 +471,8 @@ main(argc, argv)
 
     setup_signals();
 
+    create_linkpidfile(getpid());
+
     waiting = 0;
 
     /*
@@ -476,24 +482,19 @@ main(argc, argv)
        /*
         * Open the loopback channel and set it up to be the ppp interface.
         */
-#ifdef USE_TDB
-       tdb_writelock(pppdb);
-#endif
        fd_loop = open_ppp_loopback();
        set_ifunit(1);
-#ifdef USE_TDB
-       tdb_writeunlock(pppdb);
-#endif
        /*
         * Configure the interface and mark it up, etc.
         */
        demand_conf();
-       create_linkpidfile(getpid());
     }
 
     do_callback = 0;
     for (;;) {
 
+       bundle_eof = 0;
+       bundle_terminating = 0;
        listen_time = 0;
        need_holdoff = 1;
        devfd = -1;
@@ -511,13 +512,13 @@ main(argc, argv)
            add_fd(fd_loop);
            for (;;) {
                handle_events();
-               if (kill_link && !persist)
+               if (asked_to_quit)
                    break;
                if (get_loop_output())
                    break;
            }
            remove_fd(fd_loop);
-           if (kill_link && !persist)
+           if (asked_to_quit)
                break;
 
            /*
@@ -527,57 +528,22 @@ main(argc, argv)
            info("Starting link");
        }
 
-       new_phase(PHASE_SERIALCONN);
-
-       devfd = the_channel->connect();
-       if (devfd < 0)
-           goto fail;
-
-       /* set up the serial device as a ppp interface */
-#ifdef USE_TDB
-       tdb_writelock(pppdb);
-#endif
-       fd_ppp = the_channel->establish_ppp(devfd);
-       if (fd_ppp < 0) {
-#ifdef USE_TDB
-           tdb_writeunlock(pppdb);
-#endif
-           status = EXIT_FATAL_ERROR;
-           goto disconnect;
-       }
-       /* create the pid file, now that we've obtained a ppp interface */
-       if (!demand)
-           create_linkpidfile(getpid());
-
-       if (!demand && ifunit >= 0)
-           set_ifunit(1);
-#ifdef USE_TDB
-       tdb_writeunlock(pppdb);
-#endif
-
-       /*
-        * Start opening the connection and wait for
-        * incoming events (reply, timeout, etc.).
-        */
-       if (ifunit >= 0)
-               notice("Connect: %s <--> %s", ifname, ppp_devnam);
-       else
-               notice("Starting negotiation on %s", ppp_devnam);
        gettimeofday(&start_time, NULL);
        script_unsetenv("CONNECT_TIME");
        script_unsetenv("BYTES_SENT");
        script_unsetenv("BYTES_RCVD");
-       lcp_lowerup(0);
 
-       add_fd(fd_ppp);
        lcp_open(0);            /* Start protocol */
-       status = EXIT_NEGOTIATION_FAILED;
-       new_phase(PHASE_ESTABLISH);
        while (phase != PHASE_DEAD) {
            handle_events();
            get_input();
            if (kill_link)
                lcp_close(0, "User request");
+           if (asked_to_quit) {
+               bundle_terminating = 1;
+               if (phase == PHASE_MASTER)
+                   mp_bundle_terminated();
+           }
            if (open_ccp_flag) {
                if (phase == PHASE_NETWORK || phase == PHASE_RUNNING) {
                    ccp_fsm[0].flags = OPT_RESTART; /* clears OPT_SILENT */
@@ -586,55 +552,7 @@ main(argc, argv)
            }
        }
 
-       print_link_stats();
-
-       /*
-        * Delete pid file before disestablishing ppp.  Otherwise it
-        * can happen that another pppd gets the same unit and then
-        * we delete its pid file.
-        */
-       if (!demand) {
-           if (pidfilename[0] != 0
-               && unlink(pidfilename) < 0 && errno != ENOENT)
-               warn("unable to delete pid file %s: %m", pidfilename);
-           pidfilename[0] = 0;
-       }
-
-       /*
-        * If we may want to bring the link up again, transfer
-        * the ppp unit back to the loopback.  Set the
-        * real serial device back to its normal mode of operation.
-        */
-       remove_fd(fd_ppp);
-       clean_check();
-       the_channel->disestablish_ppp(devfd);
-       fd_ppp = -1;
-       if (!hungup)
-           lcp_lowerdown(0);
-       if (!demand)
-           script_unsetenv("IFNAME");
-
-       /*
-        * Run disconnector script, if requested.
-        * XXX we may not be able to do this if the line has hung up!
-        */
-    disconnect:
-       new_phase(PHASE_DISCONNECT);
-       if (the_channel->disconnect)
-           the_channel->disconnect();
-
-    fail:
-       if (the_channel->cleanup)
-           (*the_channel->cleanup)();
-
-       if (!demand) {
-           if (pidfilename[0] != 0
-               && unlink(pidfilename) < 0 && errno != ENOENT)
-               warn("unable to delete pid file %s: %m", pidfilename);
-           pidfilename[0] = 0;
-       }
-
-       if (!persist || (maxfail > 0 && unsuccess >= maxfail))
+       if (!persist || asked_to_quit || (maxfail > 0 && unsuccess >= maxfail))
            break;
 
        if (demand)
@@ -708,6 +626,7 @@ handle_events()
     if (got_sigterm) {
        info("Terminating on signal %d", got_sigterm);
        kill_link = 1;
+       asked_to_quit = 1;
        persist = 0;
        status = EXIT_USER_REQUEST;
        got_sigterm = 0;
@@ -897,7 +816,7 @@ create_pidfile(pid)
     }
 }
 
-static void
+void
 create_linkpidfile(pid)
     int pid;
 {
@@ -919,6 +838,19 @@ create_linkpidfile(pid)
     }
 }
 
+/*
+ * remove_pidfile - remove our pid files
+ */
+void remove_pidfiles()
+{
+    if (pidfilename[0] != 0 && unlink(pidfilename) < 0 && errno != ENOENT)
+       warn("unable to delete pid file %s: %m", pidfilename);
+    pidfilename[0] = 0;
+    if (linkpidfile[0] != 0 && unlink(linkpidfile) < 0 && errno != ENOENT)
+       warn("unable to delete pid file %s: %m", linkpidfile);
+    linkpidfile[0] = 0;
+}
+
 /*
  * holdoff_end - called via a timeout when the holdoff period ends.
  */
@@ -957,14 +889,54 @@ struct protocol_list {
     { 0x4b,    "SNA over 802.2" },
     { 0x4d,    "SNA" },
     { 0x4f,    "IP6 Header Compression" },
+    { 0x51,    "KNX Bridging Data" },
+    { 0x53,    "Encryption" },
+    { 0x55,    "Individual Link Encryption" },
+    { 0x57,    "IPv6" },
+    { 0x59,    "PPP Muxing" },
+    { 0x5b,    "Vendor-Specific Network Protocol" },
+    { 0x61,    "RTP IPHC Full Header" },
+    { 0x63,    "RTP IPHC Compressed TCP" },
+    { 0x65,    "RTP IPHC Compressed non-TCP" },
+    { 0x67,    "RTP IPHC Compressed UDP 8" },
+    { 0x69,    "RTP IPHC Compressed RTP 8" },
     { 0x6f,    "Stampede Bridging" },
+    { 0x73,    "MP+" },
+    { 0xc1,    "NTCITS IPI" },
     { 0xfb,    "single-link compression" },
-    { 0xfd,    "1st choice compression" },
+    { 0xfd,    "Compressed Datagram" },
     { 0x0201,  "802.1d Hello Packets" },
     { 0x0203,  "IBM Source Routing BPDU" },
     { 0x0205,  "DEC LANBridge100 Spanning Tree" },
+    { 0x0207,  "Cisco Discovery Protocol" },
+    { 0x0209,  "Netcs Twin Routing" },
+    { 0x020b,  "STP - Scheduled Transfer Protocol" },
+    { 0x020d,  "EDP - Extreme Discovery Protocol" },
+    { 0x0211,  "Optical Supervisory Channel Protocol" },
+    { 0x0213,  "Optical Supervisory Channel Protocol" },
     { 0x0231,  "Luxcom" },
     { 0x0233,  "Sigma Network Systems" },
+    { 0x0235,  "Apple Client Server Protocol" },
+    { 0x0281,  "MPLS Unicast" },
+    { 0x0283,  "MPLS Multicast" },
+    { 0x0285,  "IEEE p1284.4 standard - data packets" },
+    { 0x0287,  "ETSI TETRA Network Protocol Type 1" },
+    { 0x0289,  "Multichannel Flow Treatment Protocol" },
+    { 0x2063,  "RTP IPHC Compressed TCP No Delta" },
+    { 0x2065,  "RTP IPHC Context State" },
+    { 0x2067,  "RTP IPHC Compressed UDP 16" },
+    { 0x2069,  "RTP IPHC Compressed RTP 16" },
+    { 0x4001,  "Cray Communications Control Protocol" },
+    { 0x4003,  "CDPD Mobile Network Registration Protocol" },
+    { 0x4005,  "Expand accelerator protocol" },
+    { 0x4007,  "ODSICP NCP" },
+    { 0x4009,  "DOCSIS DLL" },
+    { 0x400B,  "Cetacean Network Detection Protocol" },
+    { 0x4021,  "Stacker LZS" },
+    { 0x4023,  "RefTek Protocol" },
+    { 0x4025,  "Fibre Channel" },
+    { 0x4027,  "EMIT Protocols" },
+    { 0x405b,  "Vendor-Specific Protocol (VSP)" },
     { 0x8021,  "Internet Protocol Control Protocol" },
     { 0x8023,  "OSI Network Layer Control Protocol" },
     { 0x8025,  "Xerox NS IDP Control Protocol" },
@@ -984,17 +956,43 @@ struct protocol_list {
     { 0x804b,  "SNA over 802.2 Control Protocol" },
     { 0x804d,  "SNA Control Protocol" },
     { 0x804f,  "IP6 Header Compression Control Protocol" },
-    { 0x006f,  "Stampede Bridging Control Protocol" },
+    { 0x8051,  "KNX Bridging Control Protocol" },
+    { 0x8053,  "Encryption Control Protocol" },
+    { 0x8055,  "Individual Link Encryption Control Protocol" },
+    { 0x8057,  "IPv6 Control Protovol" },
+    { 0x8059,  "PPP Muxing Control Protocol" },
+    { 0x805b,  "Vendor-Specific Network Control Protocol (VSNCP)" },
+    { 0x806f,  "Stampede Bridging Control Protocol" },
+    { 0x8073,  "MP+ Control Protocol" },
+    { 0x80c1,  "NTCITS IPI Control Protocol" },
     { 0x80fb,  "Single Link Compression Control Protocol" },
     { 0x80fd,  "Compression Control Protocol" },
+    { 0x8207,  "Cisco Discovery Protocol Control" },
+    { 0x8209,  "Netcs Twin Routing" },
+    { 0x820b,  "STP - Control Protocol" },
+    { 0x820d,  "EDPCP - Extreme Discovery Protocol Ctrl Prtcl" },
+    { 0x8235,  "Apple Client Server Protocol Control" },
+    { 0x8281,  "MPLSCP" },
+    { 0x8285,  "IEEE p1284.4 standard - Protocol Control" },
+    { 0x8287,  "ETSI TETRA TNP1 Control Protocol" },
+    { 0x8289,  "Multichannel Flow Treatment Protocol" },
     { 0xc021,  "Link Control Protocol" },
     { 0xc023,  "Password Authentication Protocol" },
     { 0xc025,  "Link Quality Report" },
     { 0xc027,  "Shiva Password Authentication Protocol" },
     { 0xc029,  "CallBack Control Protocol (CBCP)" },
+    { 0xc02b,  "BACP Bandwidth Allocation Control Protocol" },
+    { 0xc02d,  "BAP" },
+    { 0xc05b,  "Vendor-Specific Authentication Protocol (VSAP)" },
     { 0xc081,  "Container Control Protocol" },
     { 0xc223,  "Challenge Handshake Authentication Protocol" },
+    { 0xc225,  "RSA Authentication Protocol" },
+    { 0xc227,  "Extensible Authentication Protocol" },
+    { 0xc229,  "Mitsubishi Security Info Exch Ptcl (SIEP)" },
+    { 0xc26f,  "Stampede Bridging Authorization Protocol" },
     { 0xc281,  "Proprietary Authentication Protocol" },
+    { 0xc283,  "Proprietary Authentication Protocol" },
+    { 0xc481,  "Proprietary Node ID Authentication Protocol" },
     { 0,       NULL },
 };
 
@@ -1031,6 +1029,11 @@ get_input()
        return;
 
     if (len == 0) {
+       if (bundle_eof && multilink_master) {
+           notice("Last channel has disconnected");
+           mp_bundle_terminated();
+           return;
+       }
        notice("Modem hangup");
        hungup = 1;
        status = EXIT_HANGUP;
@@ -1159,7 +1162,8 @@ void
 die(status)
     int status;
 {
-    print_link_stats();
+    if (!doing_multilink || multilink_master)
+       print_link_stats();
     cleanup();
     notify(exitnotify, status);
     syslog(LOG_INFO, "Exit.");
@@ -1179,13 +1183,7 @@ cleanup()
        the_channel->disestablish_ppp(devfd);
     if (the_channel->cleanup)
        (*the_channel->cleanup)();
-
-    if (pidfilename[0] != 0 && unlink(pidfilename) < 0 && errno != ENOENT)
-       warn("unable to delete pid file %s: %m", pidfilename);
-    pidfilename[0] = 0;
-    if (linkpidfile[0] != 0 && unlink(linkpidfile) < 0 && errno != ENOENT)
-       warn("unable to delete pid file %s: %m", linkpidfile);
-    linkpidfile[0] = 0;
+    remove_pidfiles();
 
 #ifdef USE_TDB
     if (pppdb != NULL)
@@ -1988,6 +1986,41 @@ script_unsetenv(var)
 #endif
 }
 
+/*
+ * Any arbitrary string used as a key for locking the database.
+ * It doesn't matter what it is as long as all pppds use the same string.
+ */
+#define PPPD_LOCK_KEY  "pppd lock"
+
+/*
+ * lock_db - get an exclusive lock on the TDB database.
+ * Used to ensure atomicity of various lookup/modify operations.
+ */
+void lock_db()
+{
+#ifdef USE_TDB
+       TDB_DATA key;
+
+       key.dptr = PPPD_LOCK_KEY;
+       key.dsize = strlen(key.dptr);
+       tdb_chainlock(pppdb, key);
+#endif
+}
+
+/*
+ * unlock_db - remove the exclusive lock obtained by lock_db.
+ */
+void unlock_db()
+{
+#ifdef USE_TDB
+       TDB_DATA key;
+
+       key.dptr = PPPD_LOCK_KEY;
+       key.dsize = strlen(key.dptr);
+       tdb_chainunlock(pppdb, key);
+#endif
+}
+
 #ifdef USE_TDB
 /*
  * update_db_entry - update our entry in the database.
@@ -2004,7 +2037,7 @@ update_db_entry()
     vlen = 0;
     for (i = 0; (p = script_env[i]) != 0; ++i)
        vlen += strlen(p) + 1;
-    vbuf = malloc(vlen);
+    vbuf = malloc(vlen + 1);
     if (vbuf == 0)
        novm("database entry");
     q = vbuf;