@@ -93,8 +93,8 @@ static const unsigned char icmpv6_ra[] = {
9393/* MTU */
9494 0x05 , 0x01 , 0x00 , 0x00 , 0x00 , 0x00 , 0x05 , 0xdc ,
9595/* Prefix info*/
96- 0x03 , 0x04 , 0x40 , 0xc0 , 0xFF , 0xFF , 0xFF , 0xFF ,
97- 0xFF , 0xFF , 0xFF , 0xFF , 0x00 , 0x00 , 0x00 , 0x00 ,
96+ 0x03 , 0x04 , 0x40 , 0xc0 , 0x00 , 0x00 , 0xFF , 0xFF ,
97+ 0x00 , 0x00 , 0xFF , 0xFF , 0x00 , 0x00 , 0x00 , 0x00 ,
9898 0x3f , 0xfe , 0x05 , 0x07 , 0x00 , 0x00 , 0x00 , 0x01 ,
9999 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
100100};
@@ -1347,6 +1347,207 @@ static void test_dst_iface_scope_mcast_send(void)
13471347 net_context_put (ctx );
13481348}
13491349
1350+ #if defined(CONFIG_NET_IPV6_PE_ENABLE )
1351+ static bool is_pe_address_found (struct net_if * iface , struct in6_addr * prefix )
1352+ {
1353+ struct net_if_ipv6 * ipv6 ;
1354+ int i ;
1355+
1356+ ipv6 = iface -> config .ip .ipv6 ;
1357+
1358+ zassert_not_null (ipv6 , "IPv6 configuration is wrong for iface %p" ,
1359+ iface );
1360+
1361+ for (i = 0 ; i < NET_IF_MAX_IPV6_ADDR ; i ++ ) {
1362+ if (!ipv6 -> unicast [i ].is_used ||
1363+ ipv6 -> unicast [i ].address .family != AF_INET6 ||
1364+ !ipv6 -> unicast [i ].is_temporary ) {
1365+ continue ;
1366+ }
1367+
1368+ if (net_is_ipv6_prefix (
1369+ (u8_t * )& ipv6 -> unicast [i ].address .in6_addr ,
1370+ (u8_t * )prefix , 64 )) {
1371+ return true;
1372+ }
1373+ }
1374+
1375+ return false;
1376+ }
1377+
1378+ static void get_pe_addresses (struct net_if * iface ,
1379+ struct in6_addr * * public_addr ,
1380+ struct in6_addr * * temp_addr )
1381+ {
1382+ struct in6_addr prefix = { { { 0x3f , 0xfe , 0x05 , 0x07 , 0 , 0 , 0 , 1 ,
1383+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } } };
1384+ struct net_if_ipv6 * ipv6 ;
1385+ int i ;
1386+
1387+ ipv6 = iface -> config .ip .ipv6 ;
1388+
1389+ zassert_not_null (ipv6 , "IPv6 configuration is wrong for iface %p" ,
1390+ iface );
1391+
1392+ for (i = 0 ; i < NET_IF_MAX_IPV6_ADDR ; i ++ ) {
1393+ if (!ipv6 -> unicast [i ].is_used ||
1394+ ipv6 -> unicast [i ].address .family != AF_INET6 ) {
1395+ continue ;
1396+ }
1397+
1398+ if (net_is_ipv6_prefix (
1399+ (u8_t * )& ipv6 -> unicast [i ].address .in6_addr ,
1400+ (u8_t * )& prefix , 64 )) {
1401+ if (ipv6 -> unicast [i ].is_temporary ) {
1402+ * temp_addr =
1403+ & ipv6 -> unicast [i ].address .in6_addr ;
1404+ } else {
1405+ * public_addr =
1406+ & ipv6 -> unicast [i ].address .in6_addr ;
1407+ }
1408+ }
1409+ }
1410+ }
1411+ #endif
1412+
1413+ static void test_privacy_extension (void )
1414+ {
1415+ #if defined(CONFIG_NET_IPV6_PE_ENABLE )
1416+ struct in6_addr prefix = { { { 0x3f , 0xfe , 0x05 , 0x07 , 0 , 0 , 0 , 1 ,
1417+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } } };
1418+ struct net_if * iface = net_if_get_default ();
1419+ bool found ;
1420+
1421+ zassert_true (iface -> pe_enabled ,
1422+ "Privacy extension not enabled for iface %p" , iface );
1423+
1424+ if (IS_ENABLED (CONFIG_NET_IPV6_PE_PREFER_PUBLIC_ADDRESSES )) {
1425+ zassert_true (iface -> pe_prefer_public ,
1426+ "Prefer public flag not set correctly for "
1427+ "iface %p" , iface );
1428+ }
1429+
1430+ /* We received RA message earlier, make sure that temporary address
1431+ * is created because of that message.
1432+ */
1433+
1434+ found = is_pe_address_found (iface , & prefix );
1435+ zassert_true (found , "Temporary address not found for iface %p" , iface );
1436+ #endif
1437+ }
1438+
1439+ static void test_privacy_extension_filters (void )
1440+ {
1441+ #if defined(CONFIG_NET_IPV6_PE_ENABLE )
1442+ #if CONFIG_NET_IPV6_PE_FILTER_PREFIX_COUNT > 0
1443+ struct in6_addr prefix1 = { { { 0x3f , 0xfe , 0x05 , 0x07 , 0 , 0 , 0 , 1 ,
1444+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } } };
1445+ struct in6_addr prefix2 = { { { 0x3f , 0xfe , 0x04 , 0x07 , 0 , 0 , 0 , 1 ,
1446+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } } };
1447+ struct in6_addr prefix3 = { { { 0x3f , 0xfe , 0x03 , 0x07 , 0 , 0 , 0 , 1 ,
1448+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } } };
1449+ struct net_if * iface = net_if_get_default ();
1450+ bool found ;
1451+ int ret ;
1452+
1453+ /* First add blacklist filters */
1454+ ret = net_ipv6_pe_add_filter (& prefix1 , true);
1455+ zassert_equal (ret , 0 , "Filter cannot be added (%d)" , ret );
1456+
1457+ ret = net_ipv6_pe_add_filter (& prefix2 , true);
1458+ zassert_equal (ret , 0 , "Filter cannot be added (%d)" , ret );
1459+
1460+ ret = net_ipv6_pe_add_filter (& prefix3 , true);
1461+ zassert_true (ret < 0 , "Filter could be added" );
1462+
1463+ /* Then delete them */
1464+ ret = net_ipv6_pe_del_filter (& prefix1 );
1465+ zassert_equal (ret , 0 , "Filter cannot be deleted (%d)" , ret );
1466+
1467+ ret = net_ipv6_pe_del_filter (& prefix2 );
1468+ zassert_equal (ret , 0 , "Filter cannot be deleted (%d)" , ret );
1469+
1470+ ret = net_ipv6_pe_del_filter (& prefix2 );
1471+ zassert_true (ret < 0 , "Filter found (%d)" , ret );
1472+
1473+ /* Then add whitelist filter */
1474+ ret = net_ipv6_pe_add_filter (& prefix1 , false);
1475+ zassert_equal (ret , 0 , "Filter cannot be added (%d)" , ret );
1476+
1477+ /* Send RS again as we have now PE whitelist filter in place */
1478+ test_rs_message ();
1479+
1480+ /* IP stack needs to process the packet */
1481+ k_sleep (K_MSEC (150 ));
1482+
1483+ found = is_pe_address_found (iface , & prefix1 );
1484+ zassert_true (found , "Temporary address not found for iface %p" , iface );
1485+
1486+ /* Then try with blacklisted filter */
1487+ ret = net_ipv6_pe_del_filter (& prefix1 );
1488+ zassert_equal (ret , 0 , "Filter cannot be deleted (%d)" , ret );
1489+
1490+ ret = net_ipv6_pe_add_filter (& prefix1 , true);
1491+ zassert_equal (ret , 0 , "Filter cannot be added (%d)" , ret );
1492+
1493+ k_sleep (K_MSEC (10 ));
1494+
1495+ /* Send RS again as we have now PE blacklist filter in place */
1496+ test_rs_message ();
1497+
1498+ k_sleep (K_MSEC (150 ));
1499+
1500+ found = is_pe_address_found (iface , & prefix1 );
1501+ zassert_false (found , "Temporary address found for iface %p" , iface );
1502+
1503+ ret = net_ipv6_pe_del_filter (& prefix1 );
1504+ zassert_equal (ret , 0 , "Filter cannot be deleted (%d)" , ret );
1505+
1506+ /* Add the temp address back for the next tests */
1507+ ret = net_ipv6_pe_add_filter (& prefix1 , false);
1508+ zassert_equal (ret , 0 , "Filter cannot be added (%d)" , ret );
1509+
1510+ k_sleep (K_MSEC (50 ));
1511+
1512+ /* Send RS again as we have now PE whitelist filter in place */
1513+ test_rs_message ();
1514+
1515+ k_sleep (K_MSEC (150 ));
1516+
1517+ found = is_pe_address_found (iface , & prefix1 );
1518+ zassert_true (found , "Temporary address not found for iface %p" , iface );
1519+ #endif
1520+ #endif
1521+ }
1522+
1523+ static void test_privacy_extension_get_addr (void )
1524+ {
1525+ #if defined(CONFIG_NET_IPV6_PE_ENABLE )
1526+ struct in6_addr dst_addr = { { { 0x3f , 0xfe , 0x05 , 0x07 , 0 , 0 , 0 , 1 ,
1527+ 0 , 0 , 2 , 3 , 4 , 5 , 6 , 7 } } };
1528+ struct net_if * iface = net_if_get_default ();
1529+ struct in6_addr * public_addr = NULL ;
1530+ struct in6_addr * temp_addr = NULL ;
1531+ const struct in6_addr * src_addr ;
1532+
1533+ get_pe_addresses (iface , & public_addr , & temp_addr );
1534+
1535+ zassert_not_null (public_addr , "No public address found" );
1536+ zassert_not_null (temp_addr , "No temporary address found" );
1537+
1538+ src_addr = net_if_ipv6_select_src_addr (iface , & dst_addr );
1539+ zassert_not_null (src_addr , "No suitable source address found" );
1540+
1541+ if (iface -> pe_prefer_public ) {
1542+ zassert_true (net_ipv6_addr_cmp (src_addr , public_addr ),
1543+ "Non public address selected" );
1544+ } else {
1545+ zassert_true (net_ipv6_addr_cmp (src_addr , temp_addr ),
1546+ "Non temporary address selected" );
1547+ }
1548+ #endif
1549+ }
1550+
13501551void test_main (void )
13511552{
13521553 ztest_test_suite (test_ipv6_fn ,
@@ -1376,7 +1577,10 @@ void test_main(void)
13761577 ztest_unit_test (test_dst_zero_scope_mcast_recv ),
13771578 ztest_unit_test (test_dst_site_scope_mcast_recv_drop ),
13781579 ztest_unit_test (test_dst_site_scope_mcast_recv_ok ),
1379- ztest_unit_test (test_dst_org_scope_mcast_recv )
1580+ ztest_unit_test (test_dst_org_scope_mcast_recv ),
1581+ ztest_unit_test (test_privacy_extension ),
1582+ ztest_unit_test (test_privacy_extension_filters ),
1583+ ztest_unit_test (test_privacy_extension_get_addr )
13801584 );
13811585 ztest_run_test_suite (test_ipv6_fn );
13821586}
0 commit comments