@@ -151,6 +151,8 @@ static int emit_endpoint_added(const peer *peer);
151151static int emit_endpoint_removed (const peer * peer );
152152static int emit_net_added (ctx * ctx , int net );
153153static int emit_net_removed (ctx * ctx , int net );
154+ static int add_peer (ctx * ctx , const dest_phys * dest , mctp_eid_t eid , int net ,
155+ peer * * ret_peer );
154156static int query_peer_properties (peer * peer );
155157static int setup_added_peer (peer * peer );
156158static void add_peer_route (peer * peer );
@@ -509,6 +511,31 @@ static int reply_message_phys(ctx *ctx, int sd, const void *resp,
509511 return 0 ;
510512}
511513
514+ static int discover_peer_from_ext_addr (ctx * ctx , struct sockaddr_mctp_ext * addr )
515+ {
516+ struct peer * peer ;
517+ struct dest_phys phys ;
518+ mctp_eid_t eid ;
519+ int net ;
520+ int rc ;
521+
522+ phys .ifindex = addr -> smctp_ifindex ;
523+ memcpy (phys .hwaddr , addr -> smctp_haddr , addr -> smctp_halen );
524+ phys .hwaddr_len = addr -> smctp_halen ;
525+ eid = addr -> smctp_base .smctp_addr .s_addr ;
526+ net = addr -> smctp_base .smctp_network ;
527+
528+ rc = add_peer (ctx , & phys , eid , net , & peer );
529+ if (rc < 0 )
530+ return rc ;
531+
532+ rc = setup_added_peer (peer );
533+ if (rc < 0 )
534+ return rc ;
535+
536+ return 0 ;
537+ }
538+
512539// Handles new Incoming Set Endpoint ID request
513540static int handle_control_set_endpoint_id (ctx * ctx ,
514541 int sd , struct sockaddr_mctp_ext * addr ,
@@ -517,6 +544,8 @@ static int handle_control_set_endpoint_id(ctx *ctx,
517544 struct mctp_ctrl_cmd_set_eid * req = NULL ;
518545 struct mctp_ctrl_resp_set_eid respi = {0 }, * resp = & respi ;
519546 size_t resp_len ;
547+ mctp_eid_t eid_set ;
548+ int rc ;
520549
521550 if (buf_size < sizeof (* req )) {
522551 warnx ("short Set Endpoint ID message" );
@@ -527,12 +556,47 @@ static int handle_control_set_endpoint_id(ctx *ctx,
527556 resp -> ctrl_hdr .command_code = req -> ctrl_hdr .command_code ;
528557 resp -> ctrl_hdr .rq_dgram_inst = RQDI_RESP ;
529558 resp -> completion_code = 0 ;
530- resp -> status = 0x01 << 4 ; // Already assigned, TODO
531- resp -> eid_set = local_addr (ctx , addr -> smctp_ifindex );
532- resp -> eid_pool_size = 0 ;
533559 resp_len = sizeof (struct mctp_ctrl_resp_set_eid );
534560
535- // TODO: learn busowner route and neigh
561+ eid_set = local_addr (ctx , addr -> smctp_ifindex );
562+ if (!eid_set ) {
563+ const char * linkstr =
564+ mctp_nl_if_byindex (ctx -> nl , addr -> smctp_ifindex );
565+
566+ rc = mctp_nl_addr_add (ctx -> nl , req -> eid , linkstr );
567+ if (rc < 0 ) {
568+ warnx ("ERR: cannot add local eid %d to ifindex %d" ,
569+ req -> eid , addr -> smctp_ifindex );
570+ return rc ;
571+ }
572+
573+ rc = discover_peer_from_ext_addr (ctx , addr );
574+ if (rc < 0 ) {
575+ warnx ("ERR: cannot discover bus owner" );
576+ mctp_nl_addr_del (ctx -> nl , req -> eid , linkstr );
577+ return rc ;
578+ }
579+
580+ resp -> status = 0x00 ; // Assignment accepted
581+ resp -> eid_set = req -> eid ;
582+ resp -> eid_pool_size = 0 ;
583+ if (ctx -> verbose )
584+ fprintf (stderr , "Accepted set eid %d\n" , req -> eid );
585+
586+ } else {
587+ resp -> status = 0x01 << 4 ; // Already assigned
588+ resp -> eid_set = eid_set ;
589+ resp -> eid_pool_size = 0 ;
590+
591+ if (ctx -> verbose && req -> eid != eid_set )
592+ fprintf (stderr ,
593+ "Rejected set eid %d, already assigned with eid %d\n" ,
594+ req -> eid , eid_set );
595+ }
596+
597+ if (ctx -> verbose && !ctx -> discovered )
598+ fprintf (stderr , "Setting discovered flag to true\n" );
599+ ctx -> discovered = true;
536600
537601 return reply_message (ctx , sd , resp , resp_len , addr );
538602}
0 commit comments