Annotation of embedaddon/bird/nest/iface.c, revision 1.1.1.2

1.1       misho       1: /*
                      2:  *     BIRD -- Management of Interfaces and Neighbor Cache
                      3:  *
                      4:  *     (c) 1998--2000 Martin Mares <mj@ucw.cz>
                      5:  *
                      6:  *     Can be freely distributed and used under the terms of the GNU GPL.
                      7:  */
                      8: 
                      9: /**
                     10:  * DOC: Interfaces
                     11:  *
                     12:  * The interface module keeps track of all network interfaces in the
                     13:  * system and their addresses.
                     14:  *
                     15:  * Each interface is represented by an &iface structure which carries
                     16:  * interface capability flags (%IF_MULTIACCESS, %IF_BROADCAST etc.),
                     17:  * MTU, interface name and index and finally a linked list of network
                     18:  * prefixes assigned to the interface, each one represented by
                     19:  * struct &ifa.
                     20:  *
                     21:  * The interface module keeps a `soft-up' state for each &iface which
                     22:  * is a conjunction of link being up, the interface being of a `sane'
                     23:  * type and at least one IP address assigned to it.
                     24:  */
                     25: 
                     26: #undef LOCAL_DEBUG
                     27: 
                     28: #include "nest/bird.h"
                     29: #include "nest/iface.h"
                     30: #include "nest/protocol.h"
                     31: #include "nest/cli.h"
                     32: #include "lib/resource.h"
                     33: #include "lib/string.h"
                     34: #include "conf/conf.h"
                     35: 
                     36: static pool *if_pool;
                     37: 
                     38: list iface_list;
                     39: 
                     40: /**
                     41:  * ifa_dump - dump interface address
                     42:  * @a: interface address descriptor
                     43:  *
                     44:  * This function dumps contents of an &ifa to the debug output.
                     45:  */
                     46: void
                     47: ifa_dump(struct ifa *a)
                     48: {
                     49:   debug("\t%I, net %I/%-2d bc %I -> %I%s%s%s\n", a->ip, a->prefix, a->pxlen, a->brd, a->opposite,
                     50:        (a->flags & IF_UP) ? "" : " DOWN",
                     51:        (a->flags & IA_PRIMARY) ? "" : " SEC",
                     52:        (a->flags & IA_PEER) ? "PEER" : "");
                     53: }
                     54: 
                     55: /**
                     56:  * if_dump - dump interface
                     57:  * @i: interface to dump
                     58:  *
                     59:  * This function dumps all information associated with a given
                     60:  * network interface to the debug output.
                     61:  */
                     62: void
                     63: if_dump(struct iface *i)
                     64: {
                     65:   struct ifa *a;
                     66: 
                     67:   debug("IF%d: %s", i->index, i->name);
                     68:   if (i->flags & IF_SHUTDOWN)
                     69:     debug(" SHUTDOWN");
                     70:   if (i->flags & IF_UP)
                     71:     debug(" UP");
                     72:   else
                     73:     debug(" DOWN");
                     74:   if (i->flags & IF_ADMIN_UP)
                     75:     debug(" LINK-UP");
                     76:   if (i->flags & IF_MULTIACCESS)
                     77:     debug(" MA");
                     78:   if (i->flags & IF_BROADCAST)
                     79:     debug(" BC");
                     80:   if (i->flags & IF_MULTICAST)
                     81:     debug(" MC");
                     82:   if (i->flags & IF_LOOPBACK)
                     83:     debug(" LOOP");
                     84:   if (i->flags & IF_IGNORE)
                     85:     debug(" IGN");
                     86:   if (i->flags & IF_TMP_DOWN)
                     87:     debug(" TDOWN");
                     88:   debug(" MTU=%d\n", i->mtu);
                     89:   WALK_LIST(a, i->addrs)
                     90:     {
                     91:       ifa_dump(a);
                     92:       ASSERT((a != i->addr) == !(a->flags & IA_PRIMARY));
                     93:     }
                     94: }
                     95: 
                     96: /**
                     97:  * if_dump_all - dump all interfaces
                     98:  *
                     99:  * This function dumps information about all known network
                    100:  * interfaces to the debug output.
                    101:  */
                    102: void
                    103: if_dump_all(void)
                    104: {
                    105:   struct iface *i;
                    106: 
                    107:   debug("Known network interfaces:\n");
                    108:   WALK_LIST(i, iface_list)
                    109:     if_dump(i);
                    110:   debug("Router ID: %08x\n", config->router_id);
                    111: }
                    112: 
                    113: static inline unsigned
                    114: if_what_changed(struct iface *i, struct iface *j)
                    115: {
                    116:   unsigned c;
                    117: 
                    118:   if (((i->flags ^ j->flags) & ~(IF_UP | IF_SHUTDOWN | IF_UPDATED | IF_ADMIN_UP | IF_LINK_UP | IF_TMP_DOWN | IF_JUST_CREATED))
1.1.1.2 ! misho     119:       || (i->index != j->index) || (i->master != j->master))
1.1       misho     120:     return IF_CHANGE_TOO_MUCH;
                    121:   c = 0;
                    122:   if ((i->flags ^ j->flags) & IF_UP)
                    123:     c |= (i->flags & IF_UP) ? IF_CHANGE_DOWN : IF_CHANGE_UP;
                    124:   if ((i->flags ^ j->flags) & IF_LINK_UP)
                    125:     c |= IF_CHANGE_LINK;
                    126:   if (i->mtu != j->mtu)
                    127:     c |= IF_CHANGE_MTU;
                    128:   return c;
                    129: }
                    130: 
                    131: static inline void
                    132: if_copy(struct iface *to, struct iface *from)
                    133: {
                    134:   to->flags = from->flags | (to->flags & IF_TMP_DOWN);
                    135:   to->mtu = from->mtu;
1.1.1.2 ! misho     136:   to->master_index = from->master_index;
        !           137:   to->master = from->master;
1.1       misho     138: }
                    139: 
                    140: static inline void
                    141: ifa_send_notify(struct proto *p, unsigned c, struct ifa *a)
                    142: {
1.1.1.2 ! misho     143:   if (p->ifa_notify && (!p->vrf_set || p->vrf == a->iface->master))
1.1       misho     144:     {
                    145:       if (p->debug & D_IFACES)
1.1.1.2 ! misho     146:        log(L_TRACE "%s <%s address %I/%d on interface %s %s",
        !           147:            p->name, (a->flags & IA_PRIMARY) ? " primary" : "",
1.1       misho     148:            a->prefix, a->pxlen, a->iface->name,
                    149:            (c & IF_CHANGE_UP) ? "added" : "removed");
                    150:       p->ifa_notify(p, c, a);
                    151:     }
                    152: }
                    153: 
                    154: static void
                    155: ifa_notify_change_(unsigned c, struct ifa *a)
                    156: {
                    157:   struct proto *p;
                    158: 
                    159:   DBG("IFA change notification (%x) for %s:%I\n", c, a->iface->name, a->ip);
                    160: 
                    161:   WALK_LIST(p, active_proto_list)
                    162:     ifa_send_notify(p, c, a);
                    163: }
                    164: 
                    165: static inline void
                    166: ifa_notify_change(unsigned c, struct ifa *a)
                    167: {
                    168:   if (c & IF_CHANGE_DOWN)
                    169:     neigh_ifa_update(a);
                    170: 
                    171:   ifa_notify_change_(c, a);
                    172: 
                    173:   if (c & IF_CHANGE_UP)
                    174:     neigh_ifa_update(a);
                    175: }
                    176: 
                    177: static inline void
                    178: if_send_notify(struct proto *p, unsigned c, struct iface *i)
                    179: {
1.1.1.2 ! misho     180:   if (p->if_notify && (!p->vrf_set || p->vrf == i->master))
1.1       misho     181:     {
                    182:       if (p->debug & D_IFACES)
                    183:        log(L_TRACE "%s < interface %s %s", p->name, i->name,
                    184:            (c & IF_CHANGE_UP) ? "goes up" :
                    185:            (c & IF_CHANGE_DOWN) ? "goes down" :
                    186:            (c & IF_CHANGE_MTU) ? "changes MTU" :
                    187:            (c & IF_CHANGE_LINK) ? "changes link" :
                    188:            (c & IF_CHANGE_CREATE) ? "created" :
                    189:            "sends unknown event");
                    190:       p->if_notify(p, c, i);
                    191:     }
                    192: }
                    193: 
                    194: static void
                    195: if_notify_change(unsigned c, struct iface *i)
                    196: {
                    197:   struct proto *p;
                    198:   struct ifa *a;
                    199: 
                    200:   if (i->flags & IF_JUST_CREATED)
                    201:     {
                    202:       i->flags &= ~IF_JUST_CREATED;
                    203:       c |= IF_CHANGE_CREATE | IF_CHANGE_MTU;
                    204:     }
                    205: 
                    206:   DBG("Interface change notification (%x) for %s\n", c, i->name);
                    207: #ifdef LOCAL_DEBUG
                    208:   if_dump(i);
                    209: #endif
                    210: 
                    211:   if (c & IF_CHANGE_DOWN)
                    212:     neigh_if_down(i);
                    213: 
                    214:   if (c & IF_CHANGE_DOWN)
                    215:     WALK_LIST(a, i->addrs)
                    216:       {
                    217:        a->flags = (i->flags & ~IA_FLAGS) | (a->flags & IA_FLAGS);
                    218:        ifa_notify_change_(IF_CHANGE_DOWN, a);
                    219:       }
                    220: 
                    221:   WALK_LIST(p, active_proto_list)
                    222:     if_send_notify(p, c, i);
                    223: 
                    224:   if (c & IF_CHANGE_UP)
                    225:     WALK_LIST(a, i->addrs)
                    226:       {
                    227:        a->flags = (i->flags & ~IA_FLAGS) | (a->flags & IA_FLAGS);
                    228:        ifa_notify_change_(IF_CHANGE_UP, a);
                    229:       }
                    230: 
                    231:   if (c & IF_CHANGE_UP)
                    232:     neigh_if_up(i);
                    233: 
                    234:   if ((c & (IF_CHANGE_UP | IF_CHANGE_DOWN | IF_CHANGE_LINK)) == IF_CHANGE_LINK)
                    235:     neigh_if_link(i);
                    236: }
                    237: 
                    238: static unsigned
                    239: if_recalc_flags(struct iface *i, unsigned flags)
                    240: {
                    241:   if ((flags & (IF_SHUTDOWN | IF_TMP_DOWN)) ||
                    242:       !(flags & IF_ADMIN_UP) ||
1.1.1.2 ! misho     243:       !i->addr ||
        !           244:       (i->master_index && !i->master))
1.1       misho     245:     flags &= ~IF_UP;
                    246:   else
                    247:     flags |= IF_UP;
                    248:   return flags;
                    249: }
                    250: 
                    251: static void
                    252: if_change_flags(struct iface *i, unsigned flags)
                    253: {
                    254:   unsigned of = i->flags;
                    255: 
                    256:   i->flags = if_recalc_flags(i, flags);
                    257:   if ((i->flags ^ of) & IF_UP)
                    258:     if_notify_change((i->flags & IF_UP) ? IF_CHANGE_UP : IF_CHANGE_DOWN, i);
                    259: }
                    260: 
                    261: /**
                    262:  * if_delete - remove interface
                    263:  * @old: interface
                    264:  *
                    265:  * This function is called by the low-level platform dependent code
                    266:  * whenever it notices an interface disappears. It is just a shorthand
                    267:  * for if_update().
                    268:  */
                    269: 
                    270: void
                    271: if_delete(struct iface *old)
                    272: {
                    273:   struct iface f = {};
                    274:   strncpy(f.name, old->name, sizeof(f.name)-1);
                    275:   f.flags = IF_SHUTDOWN;
                    276:   if_update(&f);
                    277: }
                    278: 
                    279: /**
                    280:  * if_update - update interface status
                    281:  * @new: new interface status
                    282:  *
                    283:  * if_update() is called by the low-level platform dependent code
                    284:  * whenever it notices an interface change.
                    285:  *
                    286:  * There exist two types of interface updates -- synchronous and asynchronous
                    287:  * ones. In the synchronous case, the low-level code calls if_start_update(),
                    288:  * scans all interfaces reported by the OS, uses if_update() and ifa_update()
                    289:  * to pass them to the core and then it finishes the update sequence by
                    290:  * calling if_end_update(). When working asynchronously, the sysdep code
                    291:  * calls if_update() and ifa_update() whenever it notices a change.
                    292:  *
                    293:  * if_update() will automatically notify all other modules about the change.
                    294:  */
                    295: struct iface *
                    296: if_update(struct iface *new)
                    297: {
                    298:   struct iface *i;
                    299:   unsigned c;
                    300: 
                    301:   WALK_LIST(i, iface_list)
                    302:     if (!strcmp(new->name, i->name))
                    303:       {
                    304:        new->addr = i->addr;
                    305:        new->flags = if_recalc_flags(new, new->flags);
                    306:        c = if_what_changed(i, new);
                    307:        if (c & IF_CHANGE_TOO_MUCH)     /* Changed a lot, convert it to down/up */
                    308:          {
                    309:            DBG("Interface %s changed too much -- forcing down/up transition\n", i->name);
                    310:            if_change_flags(i, i->flags | IF_TMP_DOWN);
                    311:            rem_node(&i->n);
                    312:            new->addr = i->addr;
                    313:            memcpy(&new->addrs, &i->addrs, sizeof(i->addrs));
                    314:            memcpy(i, new, sizeof(*i));
                    315:            i->flags &= ~IF_UP; /* IF_TMP_DOWN will be added later */
                    316:            goto newif;
                    317:          }
                    318: 
                    319:        if_copy(i, new);
                    320:        if (c)
                    321:          if_notify_change(c, i);
                    322: 
                    323:        i->flags |= IF_UPDATED;
                    324:        return i;
                    325:       }
                    326:   i = mb_alloc(if_pool, sizeof(struct iface));
                    327:   memcpy(i, new, sizeof(*i));
                    328:   init_list(&i->addrs);
                    329: newif:
                    330:   init_list(&i->neighbors);
                    331:   i->flags |= IF_UPDATED | IF_TMP_DOWN;                /* Tmp down as we don't have addresses yet */
                    332:   add_tail(&iface_list, &i->n);
                    333:   return i;
                    334: }
                    335: 
                    336: void
                    337: if_start_update(void)
                    338: {
                    339:   struct iface *i;
                    340:   struct ifa *a;
                    341: 
                    342:   WALK_LIST(i, iface_list)
                    343:     {
                    344:       i->flags &= ~IF_UPDATED;
                    345:       WALK_LIST(a, i->addrs)
                    346:        a->flags &= ~IF_UPDATED;
                    347:     }
                    348: }
                    349: 
                    350: void
                    351: if_end_partial_update(struct iface *i)
                    352: {
                    353:   if (i->flags & IF_TMP_DOWN)
                    354:     if_change_flags(i, i->flags & ~IF_TMP_DOWN);
                    355: }
                    356: 
                    357: void
                    358: if_end_update(void)
                    359: {
                    360:   struct iface *i;
                    361:   struct ifa *a, *b;
                    362: 
                    363:   WALK_LIST(i, iface_list)
                    364:     {
                    365:       if (!(i->flags & IF_UPDATED))
                    366:        if_change_flags(i, (i->flags & ~IF_ADMIN_UP) | IF_SHUTDOWN);
                    367:       else
                    368:        {
                    369:          WALK_LIST_DELSAFE(a, b, i->addrs)
                    370:            if (!(a->flags & IF_UPDATED))
                    371:              ifa_delete(a);
                    372:          if_end_partial_update(i);
                    373:        }
                    374:     }
                    375: }
                    376: 
                    377: void
                    378: if_flush_ifaces(struct proto *p)
                    379: {
                    380:   if (p->debug & D_EVENTS)
                    381:     log(L_TRACE "%s: Flushing interfaces", p->name);
                    382:   if_start_update();
                    383:   if_end_update();
                    384: }
                    385: 
                    386: /**
                    387:  * if_feed_baby - advertise interfaces to a new protocol
                    388:  * @p: protocol to feed
                    389:  *
                    390:  * When a new protocol starts, this function sends it a series
                    391:  * of notifications about all existing interfaces.
                    392:  */
                    393: void
                    394: if_feed_baby(struct proto *p)
                    395: {
                    396:   struct iface *i;
                    397:   struct ifa *a;
                    398: 
                    399:   if (!p->if_notify && !p->ifa_notify) /* shortcut */
                    400:     return;
                    401:   DBG("Announcing interfaces to new protocol %s\n", p->name);
                    402:   WALK_LIST(i, iface_list)
                    403:     {
                    404:       if_send_notify(p, IF_CHANGE_CREATE | ((i->flags & IF_UP) ? IF_CHANGE_UP : 0), i);
                    405:       if (i->flags & IF_UP)
                    406:        WALK_LIST(a, i->addrs)
                    407:          ifa_send_notify(p, IF_CHANGE_CREATE | IF_CHANGE_UP, a);
                    408:     }
                    409: }
                    410: 
                    411: /**
                    412:  * if_find_by_index - find interface by ifindex
                    413:  * @idx: ifindex
                    414:  *
                    415:  * This function finds an &iface structure corresponding to an interface
                    416:  * of the given index @idx. Returns a pointer to the structure or %NULL
                    417:  * if no such structure exists.
                    418:  */
                    419: struct iface *
                    420: if_find_by_index(unsigned idx)
                    421: {
                    422:   struct iface *i;
                    423: 
                    424:   WALK_LIST(i, iface_list)
                    425:     if (i->index == idx && !(i->flags & IF_SHUTDOWN))
                    426:       return i;
                    427:   return NULL;
                    428: }
                    429: 
                    430: /**
                    431:  * if_find_by_name - find interface by name
                    432:  * @name: interface name
                    433:  *
                    434:  * This function finds an &iface structure corresponding to an interface
                    435:  * of the given name @name. Returns a pointer to the structure or %NULL
                    436:  * if no such structure exists.
                    437:  */
                    438: struct iface *
                    439: if_find_by_name(char *name)
                    440: {
                    441:   struct iface *i;
                    442: 
                    443:   WALK_LIST(i, iface_list)
1.1.1.2 ! misho     444:     if (!strcmp(i->name, name) && !(i->flags & IF_SHUTDOWN))
1.1       misho     445:       return i;
                    446:   return NULL;
                    447: }
                    448: 
                    449: struct iface *
                    450: if_get_by_name(char *name)
                    451: {
                    452:   struct iface *i;
                    453: 
1.1.1.2 ! misho     454:   WALK_LIST(i, iface_list)
        !           455:     if (!strcmp(i->name, name))
        !           456:       return i;
1.1       misho     457: 
                    458:   /* No active iface, create a dummy */
                    459:   i = mb_allocz(if_pool, sizeof(struct iface));
                    460:   strncpy(i->name, name, sizeof(i->name)-1);
                    461:   i->flags = IF_SHUTDOWN;
                    462:   init_list(&i->addrs);
                    463:   init_list(&i->neighbors);
                    464:   add_tail(&iface_list, &i->n);
                    465:   return i;
                    466: }
                    467: 
                    468: struct ifa *kif_choose_primary(struct iface *i);
                    469: 
                    470: static int
                    471: ifa_recalc_primary(struct iface *i)
                    472: {
1.1.1.2 ! misho     473:   struct ifa *a;
        !           474:   int c = 0;
        !           475: 
        !           476: #ifdef IPV6
        !           477:   struct ifa *ll = NULL;
        !           478: 
        !           479:   WALK_LIST(a, i->addrs)
        !           480:     if (ipa_is_link_local(a->ip) && (!ll || (a == i->llv6)))
        !           481:       ll = a;
        !           482: 
        !           483:   c = (ll != i->llv6);
        !           484:   i->llv6 = ll;
        !           485: #endif
        !           486: 
        !           487:   a = kif_choose_primary(i);
1.1       misho     488: 
                    489:   if (a == i->addr)
1.1.1.2 ! misho     490:     return c;
1.1       misho     491: 
                    492:   if (i->addr)
                    493:     i->addr->flags &= ~IA_PRIMARY;
                    494: 
                    495:   if (a)
                    496:     {
                    497:       a->flags |= IA_PRIMARY;
                    498:       rem_node(&a->n);
                    499:       add_head(&i->addrs, &a->n);
                    500:     }
                    501: 
                    502:   i->addr = a;
                    503:   return 1;
                    504: }
                    505: 
                    506: void
                    507: ifa_recalc_all_primary_addresses(void)
                    508: {
                    509:   struct iface *i;
                    510: 
                    511:   WALK_LIST(i, iface_list)
                    512:     {
                    513:       if (ifa_recalc_primary(i))
                    514:        if_change_flags(i, i->flags | IF_TMP_DOWN);
                    515:     }
                    516: }
                    517: 
                    518: static inline int
                    519: ifa_same(struct ifa *a, struct ifa *b)
                    520: {
                    521:   return ipa_equal(a->ip, b->ip) && ipa_equal(a->prefix, b->prefix) &&
                    522:     a->pxlen == b->pxlen;
                    523: }
                    524: 
                    525: 
                    526: /**
                    527:  * ifa_update - update interface address
                    528:  * @a: new interface address
                    529:  *
                    530:  * This function adds address information to a network
                    531:  * interface. It's called by the platform dependent code during
                    532:  * the interface update process described under if_update().
                    533:  */
                    534: struct ifa *
                    535: ifa_update(struct ifa *a)
                    536: {
                    537:   struct iface *i = a->iface;
                    538:   struct ifa *b;
                    539: 
                    540:   WALK_LIST(b, i->addrs)
                    541:     if (ifa_same(b, a))
                    542:       {
                    543:        if (ipa_equal(b->brd, a->brd) &&
                    544:            ipa_equal(b->opposite, a->opposite) &&
                    545:            b->scope == a->scope &&
                    546:            !((b->flags ^ a->flags) & IA_PEER))
                    547:          {
                    548:            b->flags |= IF_UPDATED;
                    549:            return b;
                    550:          }
                    551:        ifa_delete(b);
                    552:        break;
                    553:       }
                    554: 
                    555: #ifndef IPV6
                    556:   if ((i->flags & IF_BROADCAST) && !ipa_nonzero(a->brd))
                    557:     log(L_ERR "Missing broadcast address for interface %s", i->name);
                    558: #endif
                    559: 
                    560:   b = mb_alloc(if_pool, sizeof(struct ifa));
                    561:   memcpy(b, a, sizeof(struct ifa));
                    562:   add_tail(&i->addrs, &b->n);
                    563:   b->flags = (i->flags & ~IA_FLAGS) | (a->flags & IA_FLAGS);
                    564:   if (ifa_recalc_primary(i))
                    565:     if_change_flags(i, i->flags | IF_TMP_DOWN);
                    566:   if (b->flags & IF_UP)
                    567:     ifa_notify_change(IF_CHANGE_CREATE | IF_CHANGE_UP, b);
                    568:   return b;
                    569: }
                    570: 
                    571: /**
                    572:  * ifa_delete - remove interface address
                    573:  * @a: interface address
                    574:  *
                    575:  * This function removes address information from a network
                    576:  * interface. It's called by the platform dependent code during
                    577:  * the interface update process described under if_update().
                    578:  */
                    579: void
                    580: ifa_delete(struct ifa *a)
                    581: {
                    582:   struct iface *i = a->iface;
                    583:   struct ifa *b;
                    584: 
                    585:   WALK_LIST(b, i->addrs)
                    586:     if (ifa_same(b, a))
                    587:       {
                    588:        rem_node(&b->n);
                    589:        if (b->flags & IF_UP)
                    590:          {
                    591:            b->flags &= ~IF_UP;
                    592:            ifa_notify_change(IF_CHANGE_DOWN, b);
                    593:          }
1.1.1.2 ! misho     594:        if ((b->flags & IA_PRIMARY) || (b == ifa_llv6(i)))
1.1       misho     595:          {
                    596:            if_change_flags(i, i->flags | IF_TMP_DOWN);
                    597:            ifa_recalc_primary(i);
                    598:          }
                    599:        mb_free(b);
                    600:        return;
                    601:       }
                    602: }
                    603: 
                    604: u32
                    605: if_choose_router_id(struct iface_patt *mask UNUSED6, u32 old_id UNUSED6)
                    606: {
                    607: #ifndef IPV6
                    608:   struct iface *i;
                    609:   struct ifa *a, *b;
                    610: 
                    611:   b = NULL;
                    612:   WALK_LIST(i, iface_list)
                    613:     {
                    614:       if (!(i->flags & IF_ADMIN_UP) ||
                    615:          (i->flags & IF_SHUTDOWN))
                    616:        continue;
                    617: 
                    618:       WALK_LIST(a, i->addrs)
                    619:        {
                    620:          if (a->flags & IA_SECONDARY)
                    621:            continue;
                    622: 
                    623:          if (a->scope <= SCOPE_LINK)
                    624:            continue;
                    625: 
                    626:          /* Check pattern if specified */
                    627:          if (mask && !iface_patt_match(mask, i, a))
                    628:            continue;
                    629: 
                    630:          /* No pattern or pattern matched */
                    631:          if (!b || ipa_to_u32(a->ip) < ipa_to_u32(b->ip))
                    632:            b = a;
                    633:        }
                    634:     }
                    635: 
                    636:   if (!b)
                    637:     return 0;
                    638: 
                    639:   u32 id = ipa_to_u32(b->ip);
                    640:   if (id != old_id)
                    641:     log(L_INFO "Chosen router ID %R according to interface %s", id, b->iface->name);
                    642: 
                    643:   return id;
                    644: 
                    645: #else
                    646:   return 0;
                    647: #endif
                    648: }
                    649: 
                    650: /**
                    651:  * if_init - initialize interface module
                    652:  *
                    653:  * This function is called during BIRD startup to initialize
                    654:  * all data structures of the interface module.
                    655:  */
                    656: void
                    657: if_init(void)
                    658: {
                    659:   if_pool = rp_new(&root_pool, "Interfaces");
                    660:   init_list(&iface_list);
                    661:   neigh_init(if_pool);
                    662: }
                    663: 
                    664: /*
                    665:  *     Interface Pattern Lists
                    666:  */
                    667: 
                    668: int
                    669: iface_patt_match(struct iface_patt *ifp, struct iface *i, struct ifa *a)
                    670: {
                    671:   struct iface_patt_node *p;
                    672: 
                    673:   WALK_LIST(p, ifp->ipn_list)
                    674:     {
                    675:       char *t = p->pattern;
                    676:       int pos = p->positive;
                    677: 
                    678:       if (t)
                    679:        {
                    680:          if (*t == '-')
                    681:            {
                    682:              t++;
                    683:              pos = !pos;
                    684:            }
                    685: 
                    686:          if (!patmatch(t, i->name))
                    687:            continue;
                    688:        }
                    689: 
                    690:       if (p->pxlen == 0)
                    691:        return pos;
                    692: 
                    693:       if (!a)
                    694:        continue;
                    695: 
                    696:       if (ipa_in_net(a->ip, p->prefix, p->pxlen))
                    697:        return pos;
                    698: 
                    699:       if ((a->flags & IA_PEER) &&
                    700:          ipa_in_net(a->opposite, p->prefix, p->pxlen))
                    701:        return pos;
                    702: 
                    703:       continue;
                    704:     }
                    705: 
                    706:   return 0;
                    707: }
                    708: 
                    709: struct iface_patt *
                    710: iface_patt_find(list *l, struct iface *i, struct ifa *a)
                    711: {
                    712:   struct iface_patt *p;
                    713: 
                    714:   WALK_LIST(p, *l)
                    715:     if (iface_patt_match(p, i, a))
                    716:       return p;
                    717: 
                    718:   return NULL;
                    719: }
                    720: 
                    721: static int
                    722: iface_plists_equal(struct iface_patt *pa, struct iface_patt *pb)
                    723: {
                    724:   struct iface_patt_node *x, *y;
                    725: 
                    726:   x = HEAD(pa->ipn_list);
                    727:   y = HEAD(pb->ipn_list);
                    728:   while (x->n.next && y->n.next)
                    729:     {
                    730:       if ((x->positive != y->positive) ||
                    731:          (!x->pattern && y->pattern) ||        /* This nasty lines where written by me... :-( Feela */
                    732:          (!y->pattern && x->pattern) ||
                    733:          ((x->pattern != y->pattern) && strcmp(x->pattern, y->pattern)) ||
                    734:          !ipa_equal(x->prefix, y->prefix) ||
                    735:          (x->pxlen != y->pxlen))
                    736:        return 0;
                    737:       x = (void *) x->n.next;
                    738:       y = (void *) y->n.next;
                    739:     }
                    740:   return (!x->n.next && !y->n.next);
                    741: }
                    742: 
                    743: int
                    744: iface_patts_equal(list *a, list *b, int (*comp)(struct iface_patt *, struct iface_patt *))
                    745: {
                    746:   struct iface_patt *x, *y;
                    747: 
                    748:   x = HEAD(*a);
                    749:   y = HEAD(*b);
                    750:   while (x->n.next && y->n.next)
                    751:     {
                    752:       if (!iface_plists_equal(x, y) ||
                    753:          (comp && !comp(x, y)))
                    754:        return 0;
                    755:       x = (void *) x->n.next;
                    756:       y = (void *) y->n.next;
                    757:     }
                    758:   return (!x->n.next && !y->n.next);
                    759: }
                    760: 
                    761: /*
                    762:  *  CLI commands.
                    763:  */
                    764: 
                    765: static void
                    766: if_show_addr(struct ifa *a)
                    767: {
                    768:   byte opp[STD_ADDRESS_P_LENGTH + 16];
                    769: 
                    770:   if (ipa_nonzero(a->opposite))
                    771:     bsprintf(opp, ", opposite %I", a->opposite);
                    772:   else
                    773:     opp[0] = 0;
                    774:   cli_msg(-1003, "\t%I/%d (%s%s, scope %s)",
                    775:          a->ip, a->pxlen,
                    776:          (a->flags & IA_PRIMARY) ? "Primary" : (a->flags & IA_SECONDARY) ? "Secondary" : "Unselected",
                    777:          opp, ip_scope_text(a->scope));
                    778: }
                    779: 
                    780: void
                    781: if_show(void)
                    782: {
                    783:   struct iface *i;
                    784:   struct ifa *a;
                    785:   char *type;
                    786: 
                    787:   WALK_LIST(i, iface_list)
                    788:     {
                    789:       if (i->flags & IF_SHUTDOWN)
                    790:        continue;
                    791: 
1.1.1.2 ! misho     792:       char mbuf[16 + sizeof(i->name)] = {};
        !           793:       if (i->master)
        !           794:        bsprintf(mbuf, " master=%s", i->master->name);
        !           795:       else if (i->master_index)
        !           796:        bsprintf(mbuf, " master=#%u", i->master_index);
        !           797: 
        !           798:       cli_msg(-1001, "%s %s (index=%d%s)", i->name, (i->flags & IF_UP) ? "up" : "DOWN", i->index, mbuf);
1.1       misho     799:       if (!(i->flags & IF_MULTIACCESS))
                    800:        type = "PtP";
                    801:       else
                    802:        type = "MultiAccess";
                    803:       cli_msg(-1004, "\t%s%s%s Admin%s Link%s%s%s MTU=%d",
                    804:              type,
                    805:              (i->flags & IF_BROADCAST) ? " Broadcast" : "",
                    806:              (i->flags & IF_MULTICAST) ? " Multicast" : "",
                    807:              (i->flags & IF_ADMIN_UP) ? "Up" : "Down",
                    808:              (i->flags & IF_LINK_UP) ? "Up" : "Down",
                    809:              (i->flags & IF_LOOPBACK) ? " Loopback" : "",
                    810:              (i->flags & IF_IGNORE) ? " Ignored" : "",
                    811:              i->mtu);
                    812:       if (i->addr)
                    813:        if_show_addr(i->addr);
                    814:       WALK_LIST(a, i->addrs)
                    815:        if (a != i->addr)
                    816:          if_show_addr(a);
                    817:     }
                    818:   cli_msg(0, "");
                    819: }
                    820: 
                    821: void
                    822: if_show_summary(void)
                    823: {
                    824:   struct iface *i;
                    825:   byte addr[STD_ADDRESS_P_LENGTH + 16];
                    826: 
                    827:   cli_msg(-2005, "interface state address");
                    828:   WALK_LIST(i, iface_list)
                    829:     {
1.1.1.2 ! misho     830:       if (i->flags & IF_SHUTDOWN)
        !           831:        continue;
        !           832: 
1.1       misho     833:       if (i->addr)
                    834:        bsprintf(addr, "%I/%d", i->addr->ip, i->addr->pxlen);
                    835:       else
                    836:        addr[0] = 0;
                    837:       cli_msg(-1005, "%-9s %-5s %s", i->name, (i->flags & IF_UP) ? "up" : "DOWN", addr);
                    838:     }
                    839:   cli_msg(0, "");
                    840: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>