~ubuntu-branches/ubuntu/trusty/ntp/trusty-proposed

« back to all changes in this revision

Viewing changes to ntpd/ntp_proto.c

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-04-03 07:21:01 UTC
  • mfrom: (4.1.12 sid)
  • Revision ID: package-import@ubuntu.com-20130403072101-v3vt8xcs18b6uhjf
Tags: 1:4.2.6.p5+dfsg-2ubuntu1
* New upstream version, fixing build failure in raring.
* Merge with Debian; remaining changes:
  + debian/ntp.conf, debian/ntpdate.default: Change default server to
    ntp.ubuntu.com.
  + debian/ntpdate.ifup: Stop ntp before running ntpdate when an interface
    comes up, then start again afterwards.
  + debian/ntp.init, debian/rules: Only stop when entering single user mode.
  + Add enforcing AppArmor profile:
    - debian/control: Add Conflicts/Replaces on apparmor-profiles.
    - debian/control: Add Suggests on apparmor.
    - debian/ntp.dirs: Add apparmor directories.
    - debian/ntp.preinst: Force complain on certain upgrades.
    - debian/ntp.postinst: Reload apparmor profile.
    - debian/ntp.postrm: Remove the force-complain file.
    - add debian/apparmor-profile*.
    - debian/rules: install apparmor-profile and apparmor-profile.tunable.
    - debian/README.Debian: Add note on AppArmor.
  + debian/{control,rules}: Add and enable hardened build for PIE.
  + debian/apparmor-profile: Adjust location of drift files.
  + debian/rules, debian/ntp.dirs, debian/source_ntp.py: Add apport hook.
  + debian/ntpdate-debian: Disregard empty ntp.conf files.
  + debian/ntp.preinst: Remove empty /etc/ntp.conf on fresh intallation.
  + debian/ntpdate.ifup: Fix interaction with openntpd.
  + debian/source_ntp.py: Add filter on AppArmor profile names to prevent
    false positives from denials originating in other packages.
  + debian/apparmor-profile: Add samba4 ntp signing socket to ntpd apparmor
    profile.
  + debian/apparmor-profile: adjust for IPv6.

Show diffs side-by-side

added added

removed removed

Lines of Context:
77
77
double  sys_mindisp = MINDISPERSE; /* minimum distance (s) */
78
78
double  sys_maxdist = MAXDISTANCE; /* selection threshold */
79
79
double  sys_jitter;             /* system jitter */
80
 
u_long  sys_epoch;              /* last clock update time */
 
80
u_long  sys_epoch;              /* last clock update time */
81
81
static  double sys_clockhop;    /* clockhop threshold */
82
82
int     leap_tai;               /* TAI at next next leap */
83
83
u_long  leap_sec;               /* next scheduled leap from file */
125
125
                                    int);
126
126
static  void    clock_update    (struct peer *);
127
127
static  int     default_get_precision (void);
 
128
static  int     local_refid     (struct peer *);
128
129
static  int     peer_unfit      (struct peer *);
129
130
 
130
131
 
309
310
        u_int32 opcode = 0;             /* extension field opcode */
310
311
        sockaddr_u *dstadr_sin;         /* active runway */
311
312
        struct peer *peer2;             /* aux peer structure pointer */
 
313
        endpt * match_ep;               /* newpeer() local address */
312
314
        l_fp    p_org;                  /* origin timestamp */
313
315
        l_fp    p_rec;                  /* receive timestamp */
314
316
        l_fp    p_xmt;                  /* transmit timestamp */
468
470
        restrict_mask = ntp_monitor(rbufp, restrict_mask);
469
471
        if (restrict_mask & RES_LIMITED) {
470
472
                sys_limitrejected++;
471
 
                if (!(restrict_mask & RES_KOD) || hismode ==
472
 
                    MODE_BROADCAST)
 
473
                if (!(restrict_mask & RES_KOD) || MODE_BROADCAST ==
 
474
                    hismode || MODE_SERVER == hismode)
473
475
                        return;                 /* rate exceeded */
474
476
 
475
477
                if (hismode == MODE_CLIENT)
849
851
#endif /* OPENSSL */
850
852
 
851
853
                /*
 
854
                 * Broadcasts received via a multicast address may
 
855
                 * arrive after a unicast volley has begun
 
856
                 * with the same remote address.  newpeer() will not
 
857
                 * find duplicate associations on other local endpoints
 
858
                 * if a non-NULL endpoint is supplied.  multicastclient
 
859
                 * ephemeral associations are unique across all local
 
860
                 * endpoints.
 
861
                 */
 
862
                if (!(INT_MCASTOPEN & rbufp->dstadr->flags))
 
863
                        match_ep = rbufp->dstadr;
 
864
                else
 
865
                        match_ep = NULL;
 
866
 
 
867
                /*
852
868
                 * Determine whether to execute the initial volley.
853
869
                 */
854
870
                if (sys_bdelay != 0) {
867
883
                         * Do not execute the volley. Start out in
868
884
                         * broadcast client mode.
869
885
                         */
870
 
                        if ((peer = newpeer(&rbufp->recv_srcadr,
871
 
                            rbufp->dstadr, MODE_BCLIENT, hisversion,
872
 
                            pkt->ppoll, pkt->ppoll, 0, 0, 0,
873
 
                            skeyid)) == NULL) {
 
886
                        peer = newpeer(&rbufp->recv_srcadr, match_ep,
 
887
                            MODE_BCLIENT, hisversion, pkt->ppoll,
 
888
                            pkt->ppoll, FLAG_PREEMPT, MDF_BCLNT, 0,
 
889
                            skeyid);
 
890
                        if (NULL == peer) {
874
891
                                sys_restricted++;
875
892
                                return;         /* ignore duplicate */
876
893
 
889
906
                 * packet, normally 6 (64 s) and that the poll interval
890
907
                 * is fixed at this value.
891
908
                 */
892
 
                if ((peer = newpeer(&rbufp->recv_srcadr, rbufp->dstadr,
 
909
                peer = newpeer(&rbufp->recv_srcadr, match_ep,
893
910
                    MODE_CLIENT, hisversion, pkt->ppoll, pkt->ppoll,
894
 
                    FLAG_IBURST | FLAG_PREEMPT, MDF_BCLNT, 0,
895
 
                    skeyid)) == NULL) {
 
911
                    FLAG_BC_VOL | FLAG_IBURST | FLAG_PREEMPT, MDF_BCLNT,
 
912
                    0, skeyid);
 
913
                if (NULL == peer) {
896
914
                        sys_restricted++;
897
915
                        return;                 /* ignore duplicate */
898
916
                }
1495
1513
                 * timestamp. This works for both basic and interleaved
1496
1514
                 * modes.
1497
1515
                 */
1498
 
                if (peer->cast_flags & MDF_BCLNT) {
1499
 
                        peer->cast_flags &= ~MDF_BCLNT;
 
1516
                if (FLAG_BC_VOL & peer->flags) {
 
1517
                        peer->flags &= ~FLAG_BC_VOL;
1500
1518
                        peer->delay = (peer->offset - p_offset) * 2;
1501
1519
                }
1502
1520
                p_del = peer->delay;
1602
1620
         * client mode when the client is fit and the autokey dance is
1603
1621
         * complete.
1604
1622
         */
1605
 
        if ((peer->cast_flags & MDF_BCLNT) && !(peer_unfit(peer) &
1606
 
            TEST11)) {
 
1623
        if ((FLAG_BC_VOL & peer->flags) && MODE_CLIENT == peer->hmode &&
 
1624
            !(TEST11 & peer_unfit(peer))) {     /* distance exceeded */
1607
1625
#ifdef OPENSSL
1608
1626
                if (peer->flags & FLAG_SKEY) {
1609
1627
                        if (!(~peer->crypto & CRYPTO_FLAG_ALL))
1649
1667
                sys_refid = peer->refid;
1650
1668
        else
1651
1669
                sys_refid = addr2refid(&peer->srcadr);
1652
 
        dtemp = sys_jitter + fabs(sys_offset) + peer->disp + clock_phi *
 
1670
        dtemp = sys_jitter + fabs(sys_offset) + peer->disp +
 
1671
            (peer->delay + peer->rootdelay) / 2 + clock_phi *
1653
1672
            (current_time - peer->update);
1654
1673
        sys_rootdisp = dtemp + peer->rootdisp;
1655
1674
        sys_rootdelay = peer->delay + peer->rootdelay;
2016
2035
        /*
2017
2036
         * A sample consists of the offset, delay, dispersion and epoch
2018
2037
         * of arrival. The offset and delay are determined by the on-
2019
 
         * wire protcol. The dispersion grows from the last outbound
 
2038
         * wire protocol. The dispersion grows from the last outbound
2020
2039
         * packet to the arrival of this one increased by the sum of the
2021
2040
         * peer precision and the system precision as required by the
2022
2041
         * error budget. First, shift the new arrival into the shift
2120
2139
         * save the offset, delay and jitter. Note the jitter must not
2121
2140
         * be less than the precision.
2122
2141
         */
2123
 
        if (m == 0)
 
2142
        if (m == 0) {
 
2143
                clock_select();
2124
2144
                return;
 
2145
        }
2125
2146
 
2126
2147
        etemp = fabs(peer->offset - peer->filter_offset[k]);
2127
2148
        peer->offset = peer->filter_offset[k];
2202
2223
        double  high, low;
2203
2224
        double  seljitter;
2204
2225
        double  synch[NTP_MAXASSOC], error[NTP_MAXASSOC];
2205
 
        double  orphdist = 1e10;
 
2226
        double  orphmet = 2.0 * U_INT32_MAX; /* 2x is greater than */
2206
2227
        struct peer *osys_peer = NULL;
2207
2228
        struct peer *sys_prefer = NULL; /* prefer peer */
2208
2229
        struct peer *typesystem = NULL;
2267
2288
        for (n = 0; n < NTP_HASH_SIZE; n++) {
2268
2289
                for (peer = peer_hash[n]; peer != NULL; peer =
2269
2290
                    peer->next) {
2270
 
                        peer->flags &= ~FLAG_SYSPEER;
2271
 
                        peer->status = CTL_PST_SEL_REJECT;
 
2291
                        peer->new_status = CTL_PST_SEL_REJECT;
2272
2292
 
2273
2293
                        /*
2274
2294
                         * Leave the island immediately if the peer is
2278
2298
                                continue;
2279
2299
 
2280
2300
                        /*
2281
 
                         * If this is an orphan, choose the one with
2282
 
                         * the lowest metric defined as the IPv4 address
2283
 
                         * or the first 64 bits of the hashed IPv6 address.
 
2301
                         * If this peer is an orphan parent, elect the
 
2302
                         * one with the lowest metric defined as the
 
2303
                         * IPv4 address or the first 64 bits of the
 
2304
                         * hashed IPv6 address.  To ensure convergence
 
2305
                         * on the same selected orphan, consider as
 
2306
                         * well that this system may have the lowest
 
2307
                         * metric and be the orphan parent.  If this
 
2308
                         * system wins, sys_peer will be NULL to trigger
 
2309
                         * orphan mode in timer().
2284
2310
                         */
2285
2311
                        if (peer->stratum == sys_orphan) {
2286
 
                                double  ftemp;
 
2312
                                u_int32 localmet;
 
2313
                                u_int32 peermet;
2287
2314
 
2288
 
                                ftemp = addr2refid(&peer->srcadr);
2289
 
                                if (ftemp < orphdist) {
 
2315
                                if (peer->dstadr != NULL)
 
2316
                                        localmet = ntohl(peer->dstadr->addr_refid);
 
2317
                                else
 
2318
                                        localmet = U_INT32_MAX;
 
2319
                                peermet = ntohl(addr2refid(&peer->srcadr));
 
2320
                                if (peermet < localmet &&
 
2321
                                    peermet < orphmet) {
2290
2322
                                        typeorphan = peer;
2291
 
                                        orphdist = ftemp;
 
2323
                                        orphmet = peermet;
2292
2324
                                }
2293
2325
                                continue;
2294
2326
                        }
 
2327
 
 
2328
                        /*
 
2329
                         * If this peer could have the orphan parent
 
2330
                         * as a synchronization ancestor, exclude it
 
2331
                         * from selection to avoid forming a 
 
2332
                         * synchronization loop within the orphan mesh,
 
2333
                         * triggering stratum climb to infinity 
 
2334
                         * instability.  Peers at stratum higher than
 
2335
                         * the orphan stratum could have the orphan
 
2336
                         * parent in ancestry so are excluded.
 
2337
                         * See http://bugs.ntp.org/2050
 
2338
                         */
 
2339
                        if (peer->stratum > sys_orphan)
 
2340
                                continue;
2295
2341
#ifdef REFCLOCK
2296
2342
                        /*
2297
2343
                         * The following are special cases. We deal
2298
2344
                         * with them later.
2299
2345
                         */
2300
 
                        switch (peer->refclktype) { 
2301
 
                        case REFCLK_LOCALCLOCK:
2302
 
                                if (typelocal == NULL &&
2303
 
                                    !(peer->flags & FLAG_PREFER))
2304
 
                                        typelocal = peer;
2305
 
                                continue;
 
2346
                        if (!(peer->flags & FLAG_PREFER)) {
 
2347
                                switch (peer->refclktype) {
 
2348
                                case REFCLK_LOCALCLOCK:
 
2349
                                        if (typelocal == NULL)
 
2350
                                                typelocal = peer;
 
2351
                                        continue;
2306
2352
 
2307
 
                        case REFCLK_ACTS:
2308
 
                                if (typeacts == NULL &&
2309
 
                                    !(peer->flags & FLAG_PREFER))
2310
 
                                        typeacts = peer;
2311
 
                                continue;
 
2353
                                case REFCLK_ACTS:
 
2354
                                        if (typeacts == NULL)
 
2355
                                                typeacts = peer;
 
2356
                                        continue;
 
2357
                                }
2312
2358
                        }
2313
2359
#endif /* REFCLOCK */
2314
2360
 
2317
2363
                         * island, but does not yet have the immunity
2318
2364
                         * idol.
2319
2365
                         */
2320
 
                        peer->status = CTL_PST_SEL_SANE;
 
2366
                        peer->new_status = CTL_PST_SEL_SANE;
2321
2367
                        peer_list[nlist++] = peer;
2322
2368
 
2323
2369
                        /*
2386
2432
         *
2387
2433
         * Here, nlist is the number of candidates and allow is the
2388
2434
         * number of falsetickers. Upon exit, the truechimers are the
2389
 
         * susvivors with offsets not less than low and not greater than
 
2435
         * survivors with offsets not less than low and not greater than
2390
2436
         * high. There may be none of them.
2391
2437
         */
2392
2438
        low = 1e9;
2454
2500
 
2455
2501
#ifdef REFCLOCK
2456
2502
                /*
2457
 
                 * Elegible PPS peers must survive the intersection
 
2503
                 * Eligible PPS peers must survive the intersection
2458
2504
                 * algorithm. Use the first one found, but don't
2459
2505
                 * include any of them in the cluster population.
2460
2506
                 */
2508
2554
                } else if (typelocal != NULL) {
2509
2555
                        peer_list[0] = typelocal;
2510
2556
                        nlist = 1;
2511
 
                }
 
2557
                } else
2512
2558
#endif /* REFCLOCK */
2513
2559
                if (typeorphan != NULL) {
2514
2560
                        peer_list[0] = typeorphan;
2520
2566
         * Mark the candidates at this point as truechimers.
2521
2567
         */
2522
2568
        for (i = 0; i < nlist; i++) {
2523
 
                peer_list[i]->status = CTL_PST_SEL_SELCAND;
 
2569
                peer_list[i]->new_status = CTL_PST_SEL_SELCAND;
2524
2570
#ifdef DEBUG
2525
2571
                if (debug > 1)
2526
2572
                        printf("select: survivor %s %f\n",
2574
2620
                            ntoa(&peer_list[k]->srcadr), g, d);
2575
2621
#endif
2576
2622
                if (nlist > sys_maxclock)
2577
 
                        peer_list[k]->status = CTL_PST_SEL_EXCESS;
 
2623
                        peer_list[k]->new_status = CTL_PST_SEL_EXCESS;
2578
2624
                for (j = k + 1; j < nlist; j++) {
2579
2625
                        peer_list[j - 1] = peer_list[j];
2580
2626
                        synch[j - 1] = synch[j];
2598
2644
        for (i = 0; i < nlist; i++) {
2599
2645
                peer = peer_list[i];
2600
2646
                peer->unreach = 0;
2601
 
                peer->status = CTL_PST_SEL_SYNCCAND;
 
2647
                peer->new_status = CTL_PST_SEL_SYNCCAND;
2602
2648
                sys_survivors++;
2603
2649
                if (peer->leap == LEAP_ADDSECOND) {
2604
2650
                        if (peer->flags & FLAG_REFCLOCK)
2651
2697
         */
2652
2698
        if (typesystem != NULL) {
2653
2699
                if (sys_prefer == NULL) {
2654
 
                        typesystem->status = CTL_PST_SEL_SYSPEER;
 
2700
                        typesystem->new_status = CTL_PST_SEL_SYSPEER;
2655
2701
                        clock_combine(peer_list, sys_survivors);
2656
 
                        sys_jitter = SQRT(SQUARE(typesystem->jitter) +
2657
 
                            SQUARE(sys_jitter) + SQUARE(seljitter));
 
2702
                        sys_jitter = SQRT(SQUARE(sys_jitter) +
 
2703
                            SQUARE(seljitter));
2658
2704
                } else {
2659
2705
                        typesystem = sys_prefer;
2660
2706
                        sys_clockhop = 0;
2661
 
                        typesystem->status = CTL_PST_SEL_SYSPEER;
 
2707
                        typesystem->new_status = CTL_PST_SEL_SYSPEER;
2662
2708
                        sys_offset = typesystem->offset;
2663
2709
                        sys_jitter = typesystem->jitter;
2664
2710
                }
2682
2728
            NULL || (typesystem == NULL && sys_minsane == 0))))) {
2683
2729
                typesystem = typepps;
2684
2730
                sys_clockhop = 0;
2685
 
                typesystem->status = CTL_PST_SEL_PPS;
 
2731
                typesystem->new_status = CTL_PST_SEL_PPS;
2686
2732
                sys_offset = typesystem->offset;
2687
2733
                sys_jitter = typesystem->jitter;
2688
2734
#ifdef DEBUG
2702
2748
                if (osys_peer != NULL)
2703
2749
                        report_event(EVNT_NOPEER, NULL, NULL);
2704
2750
                sys_peer = NULL;                        
 
2751
                for (n = 0; n < NTP_HASH_SIZE; n++)
 
2752
                        for (peer = peer_hash[n]; peer != NULL; peer =
 
2753
                            peer->next)
 
2754
                                peer->status = peer->new_status;
2705
2755
                return;
2706
2756
        }
2707
2757
 
2715
2765
        /*
2716
2766
         * We have found the alpha male. Wind the clock.
2717
2767
         */
2718
 
        if (osys_peer != typesystem)
 
2768
        if (osys_peer != typesystem)
2719
2769
                report_event(PEVNT_NEWPEER, typesystem, NULL);
2720
 
        typesystem->flags |= FLAG_SYSPEER;
 
2770
        for (n = 0; n < NTP_HASH_SIZE; n++)
 
2771
                for (peer = peer_hash[n]; peer != NULL; peer =
 
2772
                    peer->next)
 
2773
                        peer->status = peer->new_status;
2721
2774
        clock_update(typesystem);
2722
2775
}
2723
2776
 
3382
3435
 
3383
3436
 
3384
3437
/*
 
3438
 * local_refid(peer) - check peer refid to avoid selecting peers
 
3439
 *                     currently synced to this ntpd.
 
3440
 */
 
3441
static int
 
3442
local_refid(
 
3443
        struct peer *   p
 
3444
        )
 
3445
{
 
3446
        endpt * unicast_ep;
 
3447
 
 
3448
        if (p->dstadr != NULL && !(INT_MCASTIF & p->dstadr->flags))
 
3449
                unicast_ep = p->dstadr;
 
3450
        else
 
3451
                unicast_ep = findinterface(&p->srcadr);
 
3452
 
 
3453
        if (unicast_ep != NULL && p->refid == unicast_ep->addr_refid)
 
3454
                return TRUE;
 
3455
        else
 
3456
                return FALSE;
 
3457
}
 
3458
 
 
3459
 
 
3460
/*
3385
3461
 * Determine if the peer is unfit for synchronization
3386
3462
 *
3387
3463
 * A peer is unfit for synchronization if
3421
3497
         * server as the local peer but only if the remote peer is
3422
3498
         * neither a reference clock nor an orphan.
3423
3499
         */
3424
 
        if (peer->stratum > 1 && peer->refid != htonl(LOOPBACKADR) &&
3425
 
            (peer->refid == (peer->dstadr ? peer->dstadr->addr_refid :
3426
 
            0) || peer->refid == sys_refid))
 
3500
        if (peer->stratum > 1 && local_refid(peer))
3427
3501
                rval |= TEST12;         /* synchronization loop */
3428
3502
 
3429
3503
        /*