diff options
-rw-r--r-- | include/ndp.h | 3 | ||||
-rw-r--r-- | libndp/libndp.c | 39 | ||||
-rw-r--r-- | utils/ndptool.c | 30 |
3 files changed, 61 insertions, 11 deletions
diff --git a/include/ndp.h b/include/ndp.h index 1c53358..6ce2c8b 100644 --- a/include/ndp.h +++ b/include/ndp.h @@ -86,6 +86,9 @@ void ndp_msgra_flag_other_set(struct ndp_msgra *msgra, bool flag_other); bool ndp_msgra_flag_home_agent(struct ndp_msgra *msgra); void ndp_msgra_flag_home_agent_set(struct ndp_msgra *msgra, bool flag_home_agent); +enum ndp_route_preference ndp_msgra_route_preference(struct ndp_msgra *msgra); +void ndp_msgra_route_preference_set(struct ndp_msgra *msgra, + enum ndp_route_preference pref); uint16_t ndp_msgra_router_lifetime(struct ndp_msgra *msgra); void ndp_msgra_router_lifetime_set(struct ndp_msgra *msgra, uint16_t router_lifetime); diff --git a/libndp/libndp.c b/libndp/libndp.c index 8eaa16b..631abfb 100644 --- a/libndp/libndp.c +++ b/libndp/libndp.c @@ -853,6 +853,45 @@ void ndp_msgra_flag_home_agent_set(struct ndp_msgra *msgra, } /** + * ndp_msgra_route_preference: + * @msgra: RA message structure + * + * Get route preference. + * + * Returns: route preference. + **/ +NDP_EXPORT +enum ndp_route_preference ndp_msgra_route_preference(struct ndp_msgra *msgra) +{ + uint8_t prf = (msgra->ra->nd_ra_flags_reserved >> 3) & 3; + + /* rfc4191 says: + * If the Router Lifetime is zero, the preference value MUST be set to + * (00) by the sender and MUST be ignored by the receiver. + * If the Reserved (10) value is received, the receiver MUST treat the + * value as if it were (00). + */ + if (prf == 2 || !ndp_msgra_router_lifetime(msgra)) + prf = 0; + return prf; +} + +/** + * ndp_msgra_route_preference_set: + * @msgra: RA message structure + * @pref: preference + * + * Set route preference. + **/ +NDP_EXPORT +void ndp_msgra_route_preference_set(struct ndp_msgra *msgra, + enum ndp_route_preference pref) +{ + msgra->ra->nd_ra_flags_reserved &= ~(3 << 3); + msgra->ra->nd_ra_flags_reserved |= (pref << 3); +} + +/** * ndp_msgra_router_lifetime: * @msgra: RA message structure * diff --git a/utils/ndptool.c b/utils/ndptool.c index 2898d61..8bc775b 100644 --- a/utils/ndptool.c +++ b/utils/ndptool.c @@ -164,6 +164,21 @@ static void pr_out_hwaddr(unsigned char *hwaddr, size_t len) pr_out("\n"); } +static void pr_out_route_preference(enum ndp_route_preference pref) +{ + switch (pref) { + case NDP_ROUTE_PREF_LOW: + pr_out("low"); + break; + case NDP_ROUTE_PREF_MEDIUM: + pr_out("medium"); + break; + case NDP_ROUTE_PREF_HIGH: + pr_out("high"); + break; + } +} + static int msgrcv_handler_func(struct ndp *ndp, struct ndp_msg *msg, void *priv) { char ifname[IF_NAMESIZE]; @@ -185,6 +200,9 @@ static int msgrcv_handler_func(struct ndp *ndp, struct ndp_msg *msg, void *priv) ndp_msgra_flag_managed(msgra) ? "yes" : "no"); pr_out(" Other configuration: %s\n", ndp_msgra_flag_other(msgra) ? "yes" : "no"); + pr_out(" Default router preference: "); + pr_out_route_preference(ndp_msgra_route_preference(msgra)); + pr_out("\n"); pr_out(" Router lifetime: %us\n", ndp_msgra_router_lifetime(msgra)); pr_out(" Reachable time: "); @@ -248,17 +266,7 @@ static int msgrcv_handler_func(struct ndp *ndp, struct ndp_msg *msg, void *priv) else pr_out("%us", lifetime); pr_out(", preference: "); - switch (ndp_msg_opt_route_preference(msg, offset)) { - case NDP_ROUTE_PREF_LOW: - pr_out("low"); - break; - case NDP_ROUTE_PREF_MEDIUM: - pr_out("medium"); - break; - case NDP_ROUTE_PREF_HIGH: - pr_out("high"); - break; - } + pr_out_route_preference(ndp_msg_opt_route_preference(msg, offset)); pr_out("\n"); } } else if (msg_type == NDP_MSG_NS) { |