File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / bird / proto / babel / packets.c
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Wed Mar 17 19:50:23 2021 UTC (4 years, 1 month ago) by misho
Branches: bird, MAIN
CVS tags: v1_6_8p3, HEAD
bird 1.6.8

    1: /*
    2:  *	BIRD -- The Babel protocol
    3:  *
    4:  *	Copyright (c) 2015--2016 Toke Hoiland-Jorgensen
    5:  *
    6:  *	Can be freely distributed and used under the terms of the GNU GPL.
    7:  *
    8:  *	This file contains the packet and TLV handling code for the protocol.
    9:  */
   10: 
   11: #include "babel.h"
   12: 
   13: 
   14: struct babel_pkt_header {
   15:   u8 magic;
   16:   u8 version;
   17:   u16 length;
   18: } PACKED;
   19: 
   20: struct babel_tlv {
   21:   u8 type;
   22:   u8 length;
   23:   u8 value[0];
   24: } PACKED;
   25: 
   26: struct babel_tlv_ack_req {
   27:   u8 type;
   28:   u8 length;
   29:   u16 reserved;
   30:   u16 nonce;
   31:   u16 interval;
   32: } PACKED;
   33: 
   34: struct babel_tlv_ack {
   35:   u8 type;
   36:   u8 length;
   37:   u16 nonce;
   38: } PACKED;
   39: 
   40: struct babel_tlv_hello {
   41:   u8 type;
   42:   u8 length;
   43:   u16 flags;
   44:   u16 seqno;
   45:   u16 interval;
   46: } PACKED;
   47: 
   48: struct babel_tlv_ihu {
   49:   u8 type;
   50:   u8 length;
   51:   u8 ae;
   52:   u8 reserved;
   53:   u16 rxcost;
   54:   u16 interval;
   55:   u8 addr[0];
   56: } PACKED;
   57: 
   58: struct babel_tlv_router_id {
   59:   u8 type;
   60:   u8 length;
   61:   u16 reserved;
   62:   u64 router_id;
   63: } PACKED;
   64: 
   65: struct babel_tlv_next_hop {
   66:   u8 type;
   67:   u8 length;
   68:   u8 ae;
   69:   u8 reserved;
   70:   u8 addr[0];
   71: } PACKED;
   72: 
   73: struct babel_tlv_update {
   74:   u8 type;
   75:   u8 length;
   76:   u8 ae;
   77:   u8 flags;
   78:   u8 plen;
   79:   u8 omitted;
   80:   u16 interval;
   81:   u16 seqno;
   82:   u16 metric;
   83:   u8 addr[0];
   84: } PACKED;
   85: 
   86: struct babel_tlv_route_request {
   87:   u8 type;
   88:   u8 length;
   89:   u8 ae;
   90:   u8 plen;
   91:   u8 addr[0];
   92: } PACKED;
   93: 
   94: struct babel_tlv_seqno_request {
   95:   u8 type;
   96:   u8 length;
   97:   u8 ae;
   98:   u8 plen;
   99:   u16 seqno;
  100:   u8 hop_count;
  101:   u8 reserved;
  102:   u64 router_id;
  103:   u8 addr[0];
  104: } PACKED;
  105: 
  106: 
  107: /* Hello flags */
  108: #define BABEL_HF_UNICAST	0x8000
  109: 
  110: /* Update flags */
  111: #define BABEL_UF_DEF_PREFIX	0x80
  112: #define BABEL_UF_ROUTER_ID	0x40
  113: 
  114: 
  115: struct babel_parse_state {
  116:   struct babel_proto *proto;
  117:   struct babel_iface *ifa;
  118:   ip_addr saddr;
  119:   ip_addr next_hop;
  120:   u64 router_id;		/* Router ID used in subsequent updates */
  121:   u8 def_ip6_prefix[16];	/* Implicit IPv6 prefix in network order */
  122:   u8 def_ip4_prefix[4];		/* Implicit IPv4 prefix in network order */
  123:   u8 router_id_seen;		/* router_id field is valid */
  124:   u8 def_ip6_prefix_seen;	/* def_ip6_prefix is valid */
  125:   u8 def_ip4_prefix_seen;	/* def_ip4_prefix is valid */
  126: };
  127: 
  128: enum parse_result {
  129:   PARSE_SUCCESS,
  130:   PARSE_ERROR,
  131:   PARSE_IGNORE,
  132: };
  133: 
  134: struct babel_write_state {
  135:   u64 router_id;
  136:   u8 router_id_seen;
  137: //  ip_addr next_hop;
  138: };
  139: 
  140: 
  141: #define DROP(DSC,VAL) do { err_dsc = DSC; err_val = VAL; goto drop; } while(0)
  142: #define DROP1(DSC) do { err_dsc = DSC; goto drop; } while(0)
  143: #define LOG_PKT(msg, args...) \
  144:   log_rl(&p->log_pkt_tbf, L_REMOTE "%s: " msg, p->p.name, args)
  145: 
  146: #define FIRST_TLV(p) ((struct babel_tlv *) (((struct babel_pkt_header *) p) + 1))
  147: #define NEXT_TLV(t) ((struct babel_tlv *) (((byte *) t) + TLV_LENGTH(t)))
  148: #define TLV_LENGTH(t) (t->type == BABEL_TLV_PAD1 ? 1 : t->length + sizeof(struct babel_tlv))
  149: #define TLV_OPT_LENGTH(t) (t->length + sizeof(struct babel_tlv) - sizeof(*t))
  150: #define TLV_HDR(tlv,t,l) ({ tlv->type = t; tlv->length = l - sizeof(struct babel_tlv); })
  151: #define TLV_HDR0(tlv,t) TLV_HDR(tlv, t, tlv_data[t].min_length)
  152: 
  153: static inline u16
  154: get_time16(const void *p)
  155: {
  156:   u16 v = get_u16(p) / BABEL_TIME_UNITS;
  157:   return MAX(1, v);
  158: }
  159: 
  160: static inline void
  161: put_time16(void *p, u16 v)
  162: {
  163:   put_u16(p, v * BABEL_TIME_UNITS);
  164: }
  165: 
  166: static inline ip6_addr
  167: get_ip6_px(const void *p, uint plen)
  168: {
  169:   ip6_addr addr = IPA_NONE;
  170:   memcpy(&addr, p, BYTES(plen));
  171:   return ip6_ntoh(addr);
  172: }
  173: 
  174: static inline void
  175: put_ip6_px(void *p, ip6_addr addr, uint plen)
  176: {
  177:   addr = ip6_hton(addr);
  178:   memcpy(p, &addr, BYTES(plen));
  179: }
  180: 
  181: static inline ip6_addr
  182: get_ip6_ll(const void *p)
  183: {
  184:   return ip6_build(0xfe800000, 0, get_u32(p+0), get_u32(p+4));
  185: }
  186: 
  187: static inline void
  188: put_ip6_ll(void *p, ip6_addr addr)
  189: {
  190:   put_u32(p+0, _I2(addr));
  191:   put_u32(p+4, _I3(addr));
  192: }
  193: 
  194: 
  195: /*
  196:  *	TLV read/write functions
  197:  */
  198: 
  199: static int babel_read_ack_req(struct babel_tlv *hdr, union babel_msg *msg, struct babel_parse_state *state);
  200: static int babel_read_hello(struct babel_tlv *hdr, union babel_msg *msg, struct babel_parse_state *state);
  201: static int babel_read_ihu(struct babel_tlv *hdr, union babel_msg *msg, struct babel_parse_state *state);
  202: static int babel_read_router_id(struct babel_tlv *hdr, union babel_msg *msg, struct babel_parse_state *state);
  203: static int babel_read_next_hop(struct babel_tlv *hdr, union babel_msg *msg, struct babel_parse_state *state);
  204: static int babel_read_update(struct babel_tlv *hdr, union babel_msg *msg, struct babel_parse_state *state);
  205: static int babel_read_route_request(struct babel_tlv *hdr, union babel_msg *msg, struct babel_parse_state *state);
  206: static int babel_read_seqno_request(struct babel_tlv *hdr, union babel_msg *msg, struct babel_parse_state *state);
  207: 
  208: static uint babel_write_ack(struct babel_tlv *hdr, union babel_msg *msg, struct babel_write_state *state, uint max_len);
  209: static uint babel_write_hello(struct babel_tlv *hdr, union babel_msg *msg, struct babel_write_state *state, uint max_len);
  210: static uint babel_write_ihu(struct babel_tlv *hdr, union babel_msg *msg, struct babel_write_state *state, uint max_len);
  211: static uint babel_write_update(struct babel_tlv *hdr, union babel_msg *msg, struct babel_write_state *state, uint max_len);
  212: static uint babel_write_route_request(struct babel_tlv *hdr, union babel_msg *msg, struct babel_write_state *state, uint max_len);
  213: static uint babel_write_seqno_request(struct babel_tlv *hdr, union babel_msg *msg, struct babel_write_state *state, uint max_len);
  214: 
  215: struct babel_tlv_data {
  216:   u8 min_length;
  217:   int (*read_tlv)(struct babel_tlv *hdr, union babel_msg *m, struct babel_parse_state *state);
  218:   uint (*write_tlv)(struct babel_tlv *hdr, union babel_msg *m, struct babel_write_state *state, uint max_len);
  219:   void (*handle_tlv)(union babel_msg *m, struct babel_iface *ifa);
  220: };
  221: 
  222: static const struct babel_tlv_data tlv_data[BABEL_TLV_MAX] = {
  223:   [BABEL_TLV_ACK_REQ] = {
  224:     sizeof(struct babel_tlv_ack_req),
  225:     babel_read_ack_req,
  226:     NULL,
  227:     babel_handle_ack_req
  228:   },
  229:   [BABEL_TLV_ACK] = {
  230:     sizeof(struct babel_tlv_ack),
  231:     NULL,
  232:     babel_write_ack,
  233:     NULL
  234:   },
  235:   [BABEL_TLV_HELLO] = {
  236:     sizeof(struct babel_tlv_hello),
  237:     babel_read_hello,
  238:     babel_write_hello,
  239:     babel_handle_hello
  240:   },
  241:   [BABEL_TLV_IHU] = {
  242:     sizeof(struct babel_tlv_ihu),
  243:     babel_read_ihu,
  244:     babel_write_ihu,
  245:     babel_handle_ihu
  246:   },
  247:   [BABEL_TLV_ROUTER_ID] = {
  248:     sizeof(struct babel_tlv_router_id),
  249:     babel_read_router_id,
  250:     NULL,
  251:     NULL
  252:   },
  253:   [BABEL_TLV_NEXT_HOP] = {
  254:     sizeof(struct babel_tlv_next_hop),
  255:     babel_read_next_hop,
  256:     NULL,
  257:     NULL
  258:   },
  259:   [BABEL_TLV_UPDATE] = {
  260:     sizeof(struct babel_tlv_update),
  261:     babel_read_update,
  262:     babel_write_update,
  263:     babel_handle_update
  264:   },
  265:   [BABEL_TLV_ROUTE_REQUEST] = {
  266:     sizeof(struct babel_tlv_route_request),
  267:     babel_read_route_request,
  268:     babel_write_route_request,
  269:     babel_handle_route_request
  270:   },
  271:   [BABEL_TLV_SEQNO_REQUEST] = {
  272:     sizeof(struct babel_tlv_seqno_request),
  273:     babel_read_seqno_request,
  274:     babel_write_seqno_request,
  275:     babel_handle_seqno_request
  276:   },
  277: };
  278: 
  279: static int
  280: babel_read_ack_req(struct babel_tlv *hdr, union babel_msg *m,
  281: 		   struct babel_parse_state *state)
  282: {
  283:   struct babel_tlv_ack_req *tlv = (void *) hdr;
  284:   struct babel_msg_ack_req *msg = &m->ack_req;
  285: 
  286:   msg->type = BABEL_TLV_ACK_REQ;
  287:   msg->nonce = get_u16(&tlv->nonce);
  288:   msg->interval = get_time16(&tlv->interval);
  289:   msg->sender = state->saddr;
  290: 
  291:   if (!msg->interval)
  292:     return PARSE_ERROR;
  293: 
  294:   return PARSE_SUCCESS;
  295: }
  296: 
  297: static uint
  298: babel_write_ack(struct babel_tlv *hdr, union babel_msg *m,
  299:                 struct babel_write_state *state UNUSED, uint max_len UNUSED)
  300: {
  301:   struct babel_tlv_ack *tlv = (void *) hdr;
  302:   struct babel_msg_ack *msg = &m->ack;
  303: 
  304:   TLV_HDR0(tlv, BABEL_TLV_ACK);
  305:   put_u16(&tlv->nonce, msg->nonce);
  306: 
  307:   return sizeof(struct babel_tlv_ack);
  308: }
  309: 
  310: static int
  311: babel_read_hello(struct babel_tlv *hdr, union babel_msg *m,
  312:                  struct babel_parse_state *state)
  313: {
  314:   struct babel_tlv_hello *tlv = (void *) hdr;
  315:   struct babel_msg_hello *msg = &m->hello;
  316: 
  317:   /* We currently don't support unicast Hello */
  318:   u16 flags = get_u16(&tlv->flags);
  319:   if (flags & BABEL_HF_UNICAST)
  320:     return PARSE_IGNORE;
  321: 
  322:   msg->type = BABEL_TLV_HELLO;
  323:   msg->seqno = get_u16(&tlv->seqno);
  324:   msg->interval = get_time16(&tlv->interval);
  325:   msg->sender = state->saddr;
  326: 
  327:   return PARSE_SUCCESS;
  328: }
  329: 
  330: static uint
  331: babel_write_hello(struct babel_tlv *hdr, union babel_msg *m,
  332:                   struct babel_write_state *state UNUSED, uint max_len UNUSED)
  333: {
  334:   struct babel_tlv_hello *tlv = (void *) hdr;
  335:   struct babel_msg_hello *msg = &m->hello;
  336: 
  337:   TLV_HDR0(tlv, BABEL_TLV_HELLO);
  338:   put_u16(&tlv->seqno, msg->seqno);
  339:   put_time16(&tlv->interval, msg->interval);
  340: 
  341:   return sizeof(struct babel_tlv_hello);
  342: }
  343: 
  344: static int
  345: babel_read_ihu(struct babel_tlv *hdr, union babel_msg *m,
  346:                struct babel_parse_state *state)
  347: {
  348:   struct babel_tlv_ihu *tlv = (void *) hdr;
  349:   struct babel_msg_ihu *msg = &m->ihu;
  350: 
  351:   msg->type = BABEL_TLV_IHU;
  352:   msg->ae = tlv->ae;
  353:   msg->rxcost = get_u16(&tlv->rxcost);
  354:   msg->interval = get_time16(&tlv->interval);
  355:   msg->addr = IPA_NONE;
  356:   msg->sender = state->saddr;
  357: 
  358:   if (msg->ae >= BABEL_AE_MAX)
  359:     return PARSE_IGNORE;
  360: 
  361:   // We handle link-local IPs. In every other case, the addr field will be 0 but
  362:   // validation will succeed. The handler takes care of these cases.
  363:   if (msg->ae == BABEL_AE_IP6_LL)
  364:   {
  365:     if (TLV_OPT_LENGTH(tlv) < 8)
  366:       return PARSE_ERROR;
  367: 
  368:     msg->addr = ipa_from_ip6(get_ip6_ll(&tlv->addr));
  369:   }
  370: 
  371:   return PARSE_SUCCESS;
  372: }
  373: 
  374: static uint
  375: babel_write_ihu(struct babel_tlv *hdr, union babel_msg *m,
  376:                 struct babel_write_state *state UNUSED, uint max_len)
  377: {
  378:   struct babel_tlv_ihu *tlv = (void *) hdr;
  379:   struct babel_msg_ihu *msg = &m->ihu;
  380: 
  381:   if (ipa_is_link_local(msg->addr) && max_len < sizeof(struct babel_tlv_ihu) + 8)
  382:     return 0;
  383: 
  384:   TLV_HDR0(tlv, BABEL_TLV_IHU);
  385:   put_u16(&tlv->rxcost, msg->rxcost);
  386:   put_time16(&tlv->interval, msg->interval);
  387: 
  388:   if (!ipa_is_link_local(msg->addr))
  389:   {
  390:     tlv->ae = BABEL_AE_WILDCARD;
  391:     return sizeof(struct babel_tlv_ihu);
  392:   }
  393:   put_ip6_ll(&tlv->addr, msg->addr);
  394:   tlv->ae = BABEL_AE_IP6_LL;
  395:   hdr->length += 8;
  396:   return sizeof(struct babel_tlv_ihu) + 8;
  397: }
  398: 
  399: static int
  400: babel_read_router_id(struct babel_tlv *hdr, union babel_msg *m UNUSED,
  401:                      struct babel_parse_state *state)
  402: {
  403:   struct babel_tlv_router_id *tlv = (void *) hdr;
  404: 
  405:   state->router_id = get_u64(&tlv->router_id);
  406:   state->router_id_seen = 1;
  407: 
  408:   return PARSE_IGNORE;
  409: }
  410: 
  411: /* This is called directly from babel_write_update() */
  412: static uint
  413: babel_write_router_id(struct babel_tlv *hdr, u64 router_id,
  414: 		      struct babel_write_state *state, uint max_len UNUSED)
  415: {
  416:   struct babel_tlv_router_id *tlv = (void *) hdr;
  417: 
  418:   /* We still assume that first min_length bytes are available and zeroed */
  419: 
  420:   TLV_HDR0(tlv, BABEL_TLV_ROUTER_ID);
  421:   put_u64(&tlv->router_id, router_id);
  422: 
  423:   state->router_id = router_id;
  424:   state->router_id_seen = 1;
  425: 
  426:   return sizeof(struct babel_tlv_router_id);
  427: }
  428: 
  429: static int
  430: babel_read_next_hop(struct babel_tlv *hdr, union babel_msg *m UNUSED,
  431:                     struct babel_parse_state *state)
  432: {
  433:   struct babel_tlv_next_hop *tlv = (void *) hdr;
  434: 
  435:   switch (tlv->ae)
  436:   {
  437:   case BABEL_AE_WILDCARD:
  438:     return PARSE_ERROR;
  439: 
  440:   case BABEL_AE_IP4:
  441:     /* TODO */
  442:     return PARSE_IGNORE;
  443: 
  444:   case BABEL_AE_IP6:
  445:     if (TLV_OPT_LENGTH(tlv) < sizeof(ip6_addr))
  446:       return PARSE_ERROR;
  447: 
  448:     state->next_hop = ipa_from_ip6(get_ip6(&tlv->addr));
  449:     return PARSE_IGNORE;
  450: 
  451:   case BABEL_AE_IP6_LL:
  452:     if (TLV_OPT_LENGTH(tlv) < 8)
  453:       return PARSE_ERROR;
  454: 
  455:     state->next_hop = ipa_from_ip6(get_ip6_ll(&tlv->addr));
  456:     return PARSE_IGNORE;
  457: 
  458:   default:
  459:     return PARSE_IGNORE;
  460:   }
  461: 
  462:   return PARSE_IGNORE;
  463: }
  464: 
  465: static int
  466: babel_read_update(struct babel_tlv *hdr, union babel_msg *m,
  467:                   struct babel_parse_state *state)
  468: {
  469:   struct babel_tlv_update *tlv = (void *) hdr;
  470:   struct babel_msg_update *msg = &m->update;
  471: 
  472:   msg->type = BABEL_TLV_UPDATE;
  473:   msg->interval = get_time16(&tlv->interval);
  474:   msg->seqno = get_u16(&tlv->seqno);
  475:   msg->metric = get_u16(&tlv->metric);
  476: 
  477:   /* Length of received prefix data without omitted part */
  478:   int len = BYTES(tlv->plen) - (int) tlv->omitted;
  479:   u8 buf[16] = {};
  480: 
  481:   if ((len < 0) || ((uint) len > TLV_OPT_LENGTH(tlv)))
  482:     return PARSE_ERROR;
  483: 
  484:   switch (tlv->ae)
  485:   {
  486:   case BABEL_AE_WILDCARD:
  487:     if (tlv->plen > 0)
  488:       return PARSE_ERROR;
  489: 
  490:     msg->wildcard = 1;
  491:     break;
  492: 
  493:   case BABEL_AE_IP4:
  494:     /* TODO */
  495:     return PARSE_IGNORE;
  496: 
  497:   case BABEL_AE_IP6:
  498:     if (tlv->plen > MAX_PREFIX_LENGTH)
  499:       return PARSE_ERROR;
  500: 
  501:     /* Cannot omit data if there is no saved prefix */
  502:     if (tlv->omitted && !state->def_ip6_prefix_seen)
  503:       return PARSE_ERROR;
  504: 
  505:     /* Merge saved prefix and received prefix parts */
  506:     memcpy(buf, state->def_ip6_prefix, tlv->omitted);
  507:     memcpy(buf + tlv->omitted, tlv->addr, len);
  508: 
  509:     msg->plen = tlv->plen;
  510:     msg->prefix = ipa_from_ip6(get_ip6(buf));
  511: 
  512:     if (tlv->flags & BABEL_UF_DEF_PREFIX)
  513:     {
  514:       put_ip6(state->def_ip6_prefix, msg->prefix);
  515:       state->def_ip6_prefix_seen = 1;
  516:     }
  517: 
  518:     if (tlv->flags & BABEL_UF_ROUTER_ID)
  519:     {
  520:       state->router_id = ((u64) _I2(msg->prefix)) << 32 | _I3(msg->prefix);
  521:       state->router_id_seen = 1;
  522:     }
  523:     break;
  524: 
  525:   case BABEL_AE_IP6_LL:
  526:     /* ??? */
  527:     return PARSE_IGNORE;
  528: 
  529:   default:
  530:     return PARSE_IGNORE;
  531:   }
  532: 
  533:   /* Update must have Router ID, unless it is retraction */
  534:   if (!state->router_id_seen && (msg->metric != BABEL_INFINITY))
  535:   {
  536:     DBG("Babel: No router ID seen before update\n");
  537:     return PARSE_ERROR;
  538:   }
  539: 
  540:   msg->router_id = state->router_id;
  541:   msg->next_hop = state->next_hop;
  542:   msg->sender = state->saddr;
  543: 
  544:   return PARSE_SUCCESS;
  545: }
  546: 
  547: static uint
  548: babel_write_update(struct babel_tlv *hdr, union babel_msg *m,
  549:                    struct babel_write_state *state, uint max_len)
  550: {
  551:   struct babel_tlv_update *tlv = (void *) hdr;
  552:   struct babel_msg_update *msg = &m->update;
  553:   uint len0 = 0;
  554: 
  555:   /*
  556:    * When needed, we write Router-ID TLV before Update TLV and return size of
  557:    * both of them. There is enough space for the Router-ID TLV, because
  558:    * sizeof(struct babel_tlv_router_id) == sizeof(struct babel_tlv_update).
  559:    *
  560:    * Router ID is not used for retractions, so do not us it in such case.
  561:    */
  562:   if ((msg->metric < BABEL_INFINITY) &&
  563:       (!state->router_id_seen || (msg->router_id != state->router_id)))
  564:   {
  565:     len0 = babel_write_router_id(hdr, msg->router_id, state, max_len);
  566:     tlv = (struct babel_tlv_update *) NEXT_TLV(tlv);
  567:   }
  568: 
  569:   uint len = sizeof(struct babel_tlv_update) + BYTES(msg->plen);
  570: 
  571:   if (len0 + len > max_len)
  572:     return 0;
  573: 
  574:   memset(tlv, 0, sizeof(struct babel_tlv_update));
  575:   TLV_HDR(tlv, BABEL_TLV_UPDATE, len);
  576: 
  577:   if (msg->wildcard)
  578:   {
  579:     tlv->ae = BABEL_AE_WILDCARD;
  580:     tlv->plen = 0;
  581:   }
  582:   else
  583:   {
  584:     tlv->ae = BABEL_AE_IP6;
  585:     tlv->plen = msg->plen;
  586:     put_ip6_px(tlv->addr, msg->prefix, msg->plen);
  587:   }
  588: 
  589:   put_time16(&tlv->interval, msg->interval);
  590:   put_u16(&tlv->seqno, msg->seqno);
  591:   put_u16(&tlv->metric, msg->metric);
  592: 
  593:   return len0 + len;
  594: }
  595: 
  596: static int
  597: babel_read_route_request(struct babel_tlv *hdr, union babel_msg *m,
  598:                          struct babel_parse_state *state UNUSED)
  599: {
  600:   struct babel_tlv_route_request *tlv = (void *) hdr;
  601:   struct babel_msg_route_request *msg = &m->route_request;
  602: 
  603:   msg->type = BABEL_TLV_ROUTE_REQUEST;
  604: 
  605:   switch (tlv->ae)
  606:   {
  607:   case BABEL_AE_WILDCARD:
  608:     /* Wildcard requests must have plen 0 */
  609:     if (tlv->plen > 0)
  610:       return PARSE_ERROR;
  611: 
  612:     msg->full = 1;
  613:     return PARSE_SUCCESS;
  614: 
  615:   case BABEL_AE_IP4:
  616:     /* TODO */
  617:     return PARSE_IGNORE;
  618: 
  619:   case BABEL_AE_IP6:
  620:     if (tlv->plen > MAX_PREFIX_LENGTH)
  621:       return PARSE_ERROR;
  622: 
  623:     if (TLV_OPT_LENGTH(tlv) < BYTES(tlv->plen))
  624:       return PARSE_ERROR;
  625: 
  626:     msg->plen = tlv->plen;
  627:     msg->prefix = get_ip6_px(tlv->addr, tlv->plen);
  628:     return PARSE_SUCCESS;
  629: 
  630:   case BABEL_AE_IP6_LL:
  631:     return PARSE_ERROR;
  632: 
  633:   default:
  634:     return PARSE_IGNORE;
  635:   }
  636: 
  637:   return PARSE_IGNORE;
  638: }
  639: 
  640: static uint
  641: babel_write_route_request(struct babel_tlv *hdr, union babel_msg *m,
  642: 			  struct babel_write_state *state UNUSED, uint max_len)
  643: {
  644:   struct babel_tlv_route_request *tlv = (void *) hdr;
  645:   struct babel_msg_route_request *msg = &m->route_request;
  646: 
  647:   uint len = sizeof(struct babel_tlv_route_request) + BYTES(msg->plen);
  648: 
  649:   if (len > max_len)
  650:     return 0;
  651: 
  652:   TLV_HDR(tlv, BABEL_TLV_ROUTE_REQUEST, len);
  653: 
  654:   if (msg->full)
  655:   {
  656:     tlv->ae = BABEL_AE_WILDCARD;
  657:     tlv->plen = 0;
  658:   }
  659:   else
  660:   {
  661:     tlv->ae = BABEL_AE_IP6;
  662:     tlv->plen = msg->plen;
  663:     put_ip6_px(tlv->addr, msg->prefix, msg->plen);
  664:   }
  665: 
  666:   return len;
  667: }
  668: 
  669: static int
  670: babel_read_seqno_request(struct babel_tlv *hdr, union babel_msg *m,
  671:                          struct babel_parse_state *state)
  672: {
  673:   struct babel_tlv_seqno_request *tlv = (void *) hdr;
  674:   struct babel_msg_seqno_request *msg = &m->seqno_request;
  675: 
  676:   msg->type = BABEL_TLV_SEQNO_REQUEST;
  677:   msg->seqno = get_u16(&tlv->seqno);
  678:   msg->hop_count = tlv->hop_count;
  679:   msg->router_id = get_u64(&tlv->router_id);
  680:   msg->sender = state->saddr;
  681: 
  682:   if (tlv->hop_count == 0)
  683:     return PARSE_ERROR;
  684: 
  685:   switch (tlv->ae)
  686:   {
  687:   case BABEL_AE_WILDCARD:
  688:     return PARSE_ERROR;
  689: 
  690:   case BABEL_AE_IP4:
  691:     /* TODO */
  692:     return PARSE_IGNORE;
  693: 
  694:   case BABEL_AE_IP6:
  695:     if (tlv->plen > MAX_PREFIX_LENGTH)
  696:       return PARSE_ERROR;
  697: 
  698:     if (TLV_OPT_LENGTH(tlv) < BYTES(tlv->plen))
  699:       return PARSE_ERROR;
  700: 
  701:     msg->plen = tlv->plen;
  702:     msg->prefix = get_ip6_px(tlv->addr, tlv->plen);
  703:     return PARSE_SUCCESS;
  704: 
  705:   case BABEL_AE_IP6_LL:
  706:     return PARSE_ERROR;
  707: 
  708:   default:
  709:     return PARSE_IGNORE;
  710:   }
  711: 
  712:   return PARSE_IGNORE;
  713: }
  714: 
  715: static uint
  716: babel_write_seqno_request(struct babel_tlv *hdr, union babel_msg *m,
  717: 			  struct babel_write_state *state UNUSED, uint max_len)
  718: {
  719:   struct babel_tlv_seqno_request *tlv = (void *) hdr;
  720:   struct babel_msg_seqno_request *msg = &m->seqno_request;
  721: 
  722:   uint len = sizeof(struct babel_tlv_seqno_request) + BYTES(msg->plen);
  723: 
  724:   if (len > max_len)
  725:     return 0;
  726: 
  727:   TLV_HDR(tlv, BABEL_TLV_SEQNO_REQUEST, len);
  728:   tlv->ae = BABEL_AE_IP6;
  729:   tlv->plen = msg->plen;
  730:   put_u16(&tlv->seqno, msg->seqno);
  731:   tlv->hop_count = msg->hop_count;
  732:   put_u64(&tlv->router_id, msg->router_id);
  733:   put_ip6_px(tlv->addr, msg->prefix, msg->plen);
  734: 
  735:   return len;
  736: }
  737: 
  738: static inline int
  739: babel_read_tlv(struct babel_tlv *hdr,
  740:                union babel_msg *msg,
  741:                struct babel_parse_state *state)
  742: {
  743:   if ((hdr->type <= BABEL_TLV_PADN) ||
  744:       (hdr->type >= BABEL_TLV_MAX) ||
  745:       !tlv_data[hdr->type].read_tlv)
  746:     return PARSE_IGNORE;
  747: 
  748:   if (TLV_LENGTH(hdr) < tlv_data[hdr->type].min_length)
  749:     return PARSE_ERROR;
  750: 
  751:   memset(msg, 0, sizeof(*msg));
  752:   return tlv_data[hdr->type].read_tlv(hdr, msg, state);
  753: }
  754: 
  755: static uint
  756: babel_write_tlv(struct babel_tlv *hdr,
  757: 		union babel_msg *msg,
  758: 		struct babel_write_state *state,
  759: 		uint max_len)
  760: {
  761:   if ((msg->type <= BABEL_TLV_PADN) ||
  762:       (msg->type >= BABEL_TLV_MAX) ||
  763:       !tlv_data[msg->type].write_tlv)
  764:     return 0;
  765: 
  766:   if (tlv_data[msg->type].min_length > max_len)
  767:     return 0;
  768: 
  769:   memset(hdr, 0, tlv_data[msg->type].min_length);
  770:   return tlv_data[msg->type].write_tlv(hdr, msg, state, max_len);
  771: }
  772: 
  773: 
  774: /*
  775:  *	Packet RX/TX functions
  776:  */
  777: 
  778: static int
  779: babel_send_to(struct babel_iface *ifa, ip_addr dest)
  780: {
  781:   sock *sk = ifa->sk;
  782:   struct babel_pkt_header *hdr = (void *) sk->tbuf;
  783:   int len = get_u16(&hdr->length) + sizeof(struct babel_pkt_header);
  784: 
  785:   DBG("Babel: Sending %d bytes to %I\n", len, dest);
  786:   return sk_send_to(sk, len, dest, 0);
  787: }
  788: 
  789: /**
  790:  * babel_write_queue - Write a TLV queue to a transmission buffer
  791:  * @ifa: Interface holding the transmission buffer
  792:  * @queue: TLV queue to write (containing internal-format TLVs)
  793:  *
  794:  * This function writes a packet to the interface transmission buffer with as
  795:  * many TLVs from the &queue as will fit in the buffer. It returns the number of
  796:  * bytes written (NOT counting the packet header). The function is called by
  797:  * babel_send_queue() and babel_send_unicast() to construct packets for
  798:  * transmission, and uses per-TLV helper functions to convert the
  799:  * internal-format TLVs to their wire representations.
  800:  *
  801:  * The TLVs in the queue are freed after they are written to the buffer.
  802:  */
  803: static uint
  804: babel_write_queue(struct babel_iface *ifa, list *queue)
  805: {
  806:   struct babel_proto *p = ifa->proto;
  807:   struct babel_write_state state = {};
  808: 
  809:   if (EMPTY_LIST(*queue))
  810:     return 0;
  811: 
  812:   byte *pos = ifa->sk->tbuf;
  813:   byte *end = pos + ifa->tx_length;
  814: 
  815:   struct babel_pkt_header *pkt = (void *) pos;
  816:   pkt->magic = BABEL_MAGIC;
  817:   pkt->version = BABEL_VERSION;
  818:   pkt->length = 0;
  819:   pos += sizeof(struct babel_pkt_header);
  820: 
  821:   struct babel_msg_node *msg;
  822:   WALK_LIST_FIRST(msg, *queue)
  823:   {
  824:     if (pos >= end)
  825:       break;
  826: 
  827:     int len = babel_write_tlv((struct babel_tlv *) pos, &msg->msg, &state, end - pos);
  828: 
  829:     if (!len)
  830:       break;
  831: 
  832:     pos += len;
  833:     rem_node(NODE msg);
  834:     sl_free(p->msg_slab, msg);
  835:   }
  836: 
  837:   uint plen = pos - (byte *) pkt;
  838:   put_u16(&pkt->length, plen - sizeof(struct babel_pkt_header));
  839: 
  840:   return plen;
  841: }
  842: 
  843: void
  844: babel_send_queue(void *arg)
  845: {
  846:   struct babel_iface *ifa = arg;
  847:   while ((babel_write_queue(ifa, &ifa->msg_queue) > 0) &&
  848: 	 (babel_send_to(ifa, IP6_BABEL_ROUTERS) > 0));
  849: }
  850: 
  851: static inline void
  852: babel_kick_queue(struct babel_iface *ifa)
  853: {
  854:   /*
  855:    * Only schedule send event if there is not already data in the socket buffer.
  856:    * Otherwise we may overwrite the data already in the buffer.
  857:    */
  858: 
  859:   if ((ifa->sk->tpos == ifa->sk->tbuf) && !ev_active(ifa->send_event))
  860:     ev_schedule(ifa->send_event);
  861: }
  862: 
  863: /**
  864:  * babel_send_unicast - send a single TLV via unicast to a destination
  865:  * @msg: TLV to send
  866:  * @ifa: Interface to send via
  867:  * @dest: Destination of the TLV
  868:  *
  869:  * This function is used to send a single TLV via unicast to a designated
  870:  * receiver. This is used for replying to certain incoming requests, and for
  871:  * sending unicast requests to refresh routes before they expire.
  872:  */
  873: void
  874: babel_send_unicast(union babel_msg *msg, struct babel_iface *ifa, ip_addr dest)
  875: {
  876:   struct babel_proto *p = ifa->proto;
  877:   struct babel_msg_node *msgn = sl_alloc(p->msg_slab);
  878:   list queue;
  879: 
  880:   msgn->msg = *msg;
  881:   init_list(&queue);
  882:   add_tail(&queue, NODE msgn);
  883:   babel_write_queue(ifa, &queue);
  884:   babel_send_to(ifa, dest);
  885: 
  886:   /* We could overwrite waiting packet here, we may have to kick TX queue */
  887:   if (!EMPTY_LIST(ifa->msg_queue))
  888:     babel_kick_queue(ifa);
  889: }
  890: 
  891: /**
  892:  * babel_enqueue - enqueue a TLV for transmission on an interface
  893:  * @msg: TLV to enqueue (in internal TLV format)
  894:  * @ifa: Interface to enqueue to
  895:  *
  896:  * This function is called to enqueue a TLV for subsequent transmission on an
  897:  * interface. The transmission event is triggered whenever a TLV is enqueued;
  898:  * this ensures that TLVs will be transmitted in a timely manner, but that TLVs
  899:  * which are enqueued in rapid succession can be transmitted together in one
  900:  * packet.
  901:  */
  902: void
  903: babel_enqueue(union babel_msg *msg, struct babel_iface *ifa)
  904: {
  905:   struct babel_proto *p = ifa->proto;
  906:   struct babel_msg_node *msgn = sl_alloc(p->msg_slab);
  907:   msgn->msg = *msg;
  908:   add_tail(&ifa->msg_queue, NODE msgn);
  909:   babel_kick_queue(ifa);
  910: }
  911: 
  912: /**
  913:  * babel_process_packet - process incoming data packet
  914:  * @pkt: Pointer to the packet data
  915:  * @len: Length of received packet
  916:  * @saddr: Address of packet sender
  917:  * @ifa: Interface packet was received on.
  918:  *
  919:  * This function is the main processing hook of incoming Babel packets. It
  920:  * checks that the packet header is well-formed, then processes the TLVs
  921:  * contained in the packet. This is done in two passes: First all TLVs are
  922:  * parsed into the internal TLV format. If a TLV parser fails, processing of the
  923:  * rest of the packet is aborted.
  924:  *
  925:  * After the parsing step, the TLV handlers are called for each parsed TLV in
  926:  * order.
  927:  */
  928: static void
  929: babel_process_packet(struct babel_pkt_header *pkt, int len,
  930:                      ip_addr saddr, struct babel_iface *ifa)
  931: {
  932:   struct babel_proto *p = ifa->proto;
  933:   struct babel_tlv *tlv;
  934:   struct babel_msg_node *msg;
  935:   list msgs;
  936:   int res;
  937: 
  938:   int plen = sizeof(struct babel_pkt_header) + get_u16(&pkt->length);
  939:   byte *pos;
  940:   byte *end = (byte *)pkt + plen;
  941: 
  942:   struct babel_parse_state state = {
  943:     .proto	= p,
  944:     .ifa	= ifa,
  945:     .saddr	= saddr,
  946:     .next_hop	= saddr,
  947:   };
  948: 
  949:   if ((pkt->magic != BABEL_MAGIC) || (pkt->version != BABEL_VERSION))
  950:   {
  951:     TRACE(D_PACKETS, "Strange packet from %I via %s - magic %d version %d",
  952: 	  saddr, ifa->iface->name, pkt->magic, pkt->version);
  953:     return;
  954:   }
  955: 
  956:   if (plen > len)
  957:   {
  958:     LOG_PKT("Bad packet from %I via %s - %s (%u)",
  959: 	    saddr, ifa->iface->name, "length mismatch", plen);
  960:     return;
  961:   }
  962: 
  963:   TRACE(D_PACKETS, "Packet received from %I via %s",
  964:         saddr, ifa->iface->name);
  965: 
  966:   init_list(&msgs);
  967: 
  968:   /* First pass through the packet TLV by TLV, parsing each into internal data
  969:      structures. */
  970:   for (tlv = FIRST_TLV(pkt);
  971:        (byte *)tlv < end;
  972:        tlv = NEXT_TLV(tlv))
  973:   {
  974:     /* Ugly special case */
  975:     if (tlv->type == BABEL_TLV_PAD1)
  976:       continue;
  977: 
  978:     /* The end of the common TLV header */
  979:     pos = (byte *)tlv + sizeof(struct babel_tlv);
  980:     if ((pos > end) || (pos + tlv->length > end))
  981:     {
  982:       LOG_PKT("Bad TLV from %I via %s type %d pos %d - framing error",
  983: 	      saddr, ifa->iface->name, tlv->type, (byte *)tlv - (byte *)pkt);
  984:       break;
  985:     }
  986: 
  987:     msg = sl_alloc(p->msg_slab);
  988:     res = babel_read_tlv(tlv, &msg->msg, &state);
  989:     if (res == PARSE_SUCCESS)
  990:     {
  991:       add_tail(&msgs, NODE msg);
  992:     }
  993:     else if (res == PARSE_IGNORE)
  994:     {
  995:       DBG("Babel: Ignoring TLV of type %d\n", tlv->type);
  996:       sl_free(p->msg_slab, msg);
  997:     }
  998:     else /* PARSE_ERROR */
  999:     {
 1000:       LOG_PKT("Bad TLV from %I via %s type %d pos %d - parse error",
 1001: 	      saddr, ifa->iface->name, tlv->type, (byte *)tlv - (byte *)pkt);
 1002:       sl_free(p->msg_slab, msg);
 1003:       break;
 1004:     }
 1005:   }
 1006: 
 1007:   /* Parsing done, handle all parsed TLVs */
 1008:   WALK_LIST_FIRST(msg, msgs)
 1009:   {
 1010:     if (tlv_data[msg->msg.type].handle_tlv)
 1011:       tlv_data[msg->msg.type].handle_tlv(&msg->msg, ifa);
 1012:     rem_node(NODE msg);
 1013:     sl_free(p->msg_slab, msg);
 1014:   }
 1015: }
 1016: 
 1017: static void
 1018: babel_err_hook(sock *sk, int err)
 1019: {
 1020:   struct babel_iface *ifa = sk->data;
 1021:   struct babel_proto *p = ifa->proto;
 1022: 
 1023:   log(L_ERR "%s: Socket error on %s: %M", p->p.name, ifa->iface->name, err);
 1024:   /* FIXME: Drop queued TLVs here? */
 1025: }
 1026: 
 1027: 
 1028: static void
 1029: babel_tx_hook(sock *sk)
 1030: {
 1031:   struct babel_iface *ifa = sk->data;
 1032: 
 1033:   DBG("Babel: TX hook called (iface %s, src %I, dst %I)\n",
 1034:       sk->iface->name, sk->saddr, sk->daddr);
 1035: 
 1036:   babel_send_queue(ifa);
 1037: }
 1038: 
 1039: 
 1040: static int
 1041: babel_rx_hook(sock *sk, uint len)
 1042: {
 1043:   struct babel_iface *ifa = sk->data;
 1044:   struct babel_proto *p = ifa->proto;
 1045:   const char *err_dsc = NULL;
 1046:   uint err_val = 0;
 1047: 
 1048:   if (sk->lifindex != ifa->iface->index)
 1049:     return 1;
 1050: 
 1051:   DBG("Babel: RX hook called (iface %s, src %I, dst %I)\n",
 1052:       sk->iface->name, sk->faddr, sk->laddr);
 1053: 
 1054:   /* Silently ignore my own packets */
 1055:   if (ipa_equal(ifa->iface->addr->ip, sk->faddr))
 1056:     return 1;
 1057: 
 1058:   if (!ipa_is_link_local(sk->faddr))
 1059:     DROP1("wrong src address");
 1060: 
 1061:   if (sk->fport != ifa->cf->port)
 1062:     DROP("wrong src port", sk->fport);
 1063: 
 1064:   if (len < sizeof(struct babel_pkt_header))
 1065:     DROP("too short", len);
 1066: 
 1067:   if (sk->flags & SKF_TRUNCATED)
 1068:     DROP("truncated", len);
 1069: 
 1070:   babel_process_packet((struct babel_pkt_header *) sk->rbuf, len, sk->faddr, ifa);
 1071:   return 1;
 1072: 
 1073: drop:
 1074:   LOG_PKT("Bad packet from %I via %s - %s (%u)",
 1075: 	  sk->faddr, sk->iface->name, err_dsc, err_val);
 1076:   return 1;
 1077: }
 1078: 
 1079: int
 1080: babel_open_socket(struct babel_iface *ifa)
 1081: {
 1082:   struct babel_proto *p = ifa->proto;
 1083: 
 1084:   sock *sk;
 1085:   sk = sk_new(ifa->pool);
 1086:   sk->type = SK_UDP;
 1087:   sk->sport = ifa->cf->port;
 1088:   sk->dport = ifa->cf->port;
 1089:   sk->iface = ifa->iface;
 1090:   sk->saddr = ifa->addr;
 1091:   sk->vrf = p->p.vrf;
 1092: 
 1093:   sk->rx_hook = babel_rx_hook;
 1094:   sk->tx_hook = babel_tx_hook;
 1095:   sk->err_hook = babel_err_hook;
 1096:   sk->data = ifa;
 1097: 
 1098:   sk->tos = ifa->cf->tx_tos;
 1099:   sk->priority = ifa->cf->tx_priority;
 1100:   sk->ttl = 1;
 1101:   sk->flags = SKF_LADDR_RX;
 1102: 
 1103:   if (sk_open(sk) < 0)
 1104:     goto err;
 1105: 
 1106:   if (sk_setup_multicast(sk) < 0)
 1107:     goto err;
 1108: 
 1109:   if (sk_join_group(sk, IP6_BABEL_ROUTERS) < 0)
 1110:     goto err;
 1111: 
 1112:   ifa->sk = sk;
 1113:   return 1;
 1114: 
 1115: err:
 1116:   sk_log_error(sk, p->p.name);
 1117:   rfree(sk);
 1118:   return 0;
 1119: }

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