| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: config.c,v 1.43 2019/11/11 13:42:49 roy Exp $ */ | | 1 | /* $NetBSD: config.c,v 1.44 2020/04/21 12:16:47 roy Exp $ */ |
2 | /* $KAME: config.c,v 1.93 2005/10/17 14:40:02 suz Exp $ */ | | 2 | /* $KAME: config.c,v 1.93 2005/10/17 14:40:02 suz Exp $ */ |
3 | | | 3 | |
4 | /* | | 4 | /* |
5 | * Copyright (C) 1998 WIDE Project. | | 5 | * Copyright (C) 1998 WIDE Project. |
6 | * All rights reserved. | | 6 | * All rights reserved. |
7 | * | | 7 | * |
8 | * Redistribution and use in source and binary forms, with or without | | 8 | * Redistribution and use in source and binary forms, with or without |
9 | * modification, are permitted provided that the following conditions | | 9 | * modification, are permitted provided that the following conditions |
10 | * are met: | | 10 | * are met: |
11 | * 1. Redistributions of source code must retain the above copyright | | 11 | * 1. Redistributions of source code must retain the above copyright |
12 | * notice, this list of conditions and the following disclaimer. | | 12 | * notice, this list of conditions and the following disclaimer. |
13 | * 2. Redistributions in binary form must reproduce the above copyright | | 13 | * 2. Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer in the | | 14 | * notice, this list of conditions and the following disclaimer in the |
| @@ -865,69 +865,26 @@ get_prefix(struct rainfo *rai) | | | @@ -865,69 +865,26 @@ get_prefix(struct rainfo *rai) |
865 | } | | 865 | } |
866 | | | 866 | |
867 | static void | | 867 | static void |
868 | makeentry(char *buf, size_t len, int id, const char *string) | | 868 | makeentry(char *buf, size_t len, int id, const char *string) |
869 | { | | 869 | { |
870 | | | 870 | |
871 | if (id < 0) | | 871 | if (id < 0) |
872 | strlcpy(buf, string, len); | | 872 | strlcpy(buf, string, len); |
873 | else | | 873 | else |
874 | snprintf(buf, len, "%s%d", string, id); | | 874 | snprintf(buf, len, "%s%d", string, id); |
875 | } | | 875 | } |
876 | | | 876 | |
877 | /* | | 877 | /* |
878 | * Add a prefix to the list of specified interface and reconstruct | | | |
879 | * the outgoing packet. | | | |
880 | * The prefix must not be in the list. | | | |
881 | * XXX: other parameters of the prefix(e.g. lifetime) should be | | | |
882 | * able to be specified. | | | |
883 | */ | | | |
884 | static void | | | |
885 | add_prefix(struct rainfo *rai, struct in6_prefixreq *ipr) | | | |
886 | { | | | |
887 | struct prefix *prefix; | | | |
888 | char ntopbuf[INET6_ADDRSTRLEN]; | | | |
889 | | | | |
890 | if ((prefix = calloc(1, sizeof(*prefix))) == NULL) { | | | |
891 | logit(LOG_ERR, "<%s> memory allocation failed", | | | |
892 | __func__); | | | |
893 | return; /* XXX: error or exit? */ | | | |
894 | } | | | |
895 | prefix->prefix = ipr->ipr_prefix.sin6_addr; | | | |
896 | prefix->prefixlen = ipr->ipr_plen; | | | |
897 | prefix->validlifetime = ipr->ipr_vltime; | | | |
898 | prefix->preflifetime = ipr->ipr_pltime; | | | |
899 | prefix->onlinkflg = ipr->ipr_raf_onlink; | | | |
900 | prefix->autoconfflg = ipr->ipr_raf_auto; | | | |
901 | prefix->origin = PREFIX_FROM_DYNAMIC; | | | |
902 | | | | |
903 | prefix->rainfo = rai; | | | |
904 | TAILQ_INSERT_TAIL(&rai->prefix, prefix, next); | | | |
905 | rai->pfxs++; | | | |
906 | | | | |
907 | logit(LOG_DEBUG, "<%s> new prefix %s/%d was added on %s", | | | |
908 | __func__, inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr, | | | |
909 | ntopbuf, INET6_ADDRSTRLEN), | | | |
910 | ipr->ipr_plen, rai->ifname); | | | |
911 | | | | |
912 | /* free the previous packet */ | | | |
913 | free(rai->ra_data); | | | |
914 | rai->ra_data = NULL; | | | |
915 | | | | |
916 | /* reconstruct the packet */ | | | |
917 | make_packet(rai); | | | |
918 | } | | | |
919 | | | | |
920 | /* | | | |
921 | * Delete a prefix to the list of specified interface and reconstruct | | 878 | * Delete a prefix to the list of specified interface and reconstruct |
922 | * the outgoing packet. | | 879 | * the outgoing packet. |
923 | * The prefix must be in the list. | | 880 | * The prefix must be in the list. |
924 | */ | | 881 | */ |
925 | void | | 882 | void |
926 | delete_prefix(struct prefix *prefix) | | 883 | delete_prefix(struct prefix *prefix) |
927 | { | | 884 | { |
928 | char ntopbuf[INET6_ADDRSTRLEN]; | | 885 | char ntopbuf[INET6_ADDRSTRLEN]; |
929 | struct rainfo *rai = prefix->rainfo; | | 886 | struct rainfo *rai = prefix->rainfo; |
930 | | | 887 | |
931 | TAILQ_REMOVE(&rai->prefix, prefix, next); | | 888 | TAILQ_REMOVE(&rai->prefix, prefix, next); |
932 | rai->pfxs--; | | 889 | rai->pfxs--; |
933 | logit(LOG_DEBUG, "<%s> prefix %s/%d was deleted on %s", | | 890 | logit(LOG_DEBUG, "<%s> prefix %s/%d was deleted on %s", |
| @@ -991,93 +948,65 @@ update_prefix(struct prefix * prefix) | | | @@ -991,93 +948,65 @@ update_prefix(struct prefix * prefix) |
991 | __func__); | | 948 | __func__); |
992 | exit(1); | | 949 | exit(1); |
993 | } | | 950 | } |
994 | | | 951 | |
995 | logit(LOG_DEBUG, "<%s> prefix %s/%d was re-enabled on %s", | | 952 | logit(LOG_DEBUG, "<%s> prefix %s/%d was re-enabled on %s", |
996 | __func__, inet_ntop(AF_INET6, &prefix->prefix, ntopbuf, | | 953 | __func__, inet_ntop(AF_INET6, &prefix->prefix, ntopbuf, |
997 | INET6_ADDRSTRLEN), prefix->prefixlen, rai->ifname); | | 954 | INET6_ADDRSTRLEN), prefix->prefixlen, rai->ifname); |
998 | | | 955 | |
999 | /* stop the expiration timer */ | | 956 | /* stop the expiration timer */ |
1000 | rtadvd_remove_timer(&prefix->timer); | | 957 | rtadvd_remove_timer(&prefix->timer); |
1001 | } | | 958 | } |
1002 | | | 959 | |
1003 | /* | | 960 | /* |
1004 | * Try to get an in6_prefixreq contents for a prefix which matches | | 961 | * Add a prefix to the list of specified interface and reconstruct |
1005 | * ipr->ipr_prefix and ipr->ipr_plen and belongs to | | 962 | * the outgoing packet. |
1006 | * the interface whose name is ipr->ipr_name[]. | | 963 | * The prefix must not be in the list. |
| | | 964 | * XXX: other parameters of the prefix(e.g. lifetime) should be |
| | | 965 | * able to be specified. |
1007 | */ | | 966 | */ |
1008 | static int | | 967 | void |
1009 | init_prefix(struct in6_prefixreq *ipr) | | 968 | add_prefix(struct rainfo *rai, int ifindex, struct in6_addr *addr, int plen) |
1010 | { | | 969 | { |
1011 | #if 0 | | 970 | struct prefix *prefix; |
1012 | int s; | | 971 | char ntopbuf[INET6_ADDRSTRLEN]; |
1013 | | | 972 | |
1014 | if ((s = prog_socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { | | 973 | if ((prefix = calloc(1, sizeof(*prefix))) == NULL) { |
1015 | logit(LOG_ERR, "<%s> socket: %m", __func__); | | 974 | logit(LOG_ERR, "<%s> memory allocation failed", |
1016 | exit(1); | | 975 | __func__); |
| | | 976 | return; /* XXX: error or exit? */ |
1017 | } | | 977 | } |
| | | 978 | prefix->prefix = *addr; |
| | | 979 | prefix->prefixlen = plen; |
| | | 980 | prefix->validlifetime = DEF_ADVVALIDLIFETIME; |
| | | 981 | prefix->preflifetime = DEF_ADVPREFERREDLIFETIME; |
| | | 982 | prefix->onlinkflg = 1; |
| | | 983 | prefix->autoconfflg = 0; |
| | | 984 | prefix->origin = PREFIX_FROM_DYNAMIC; |
1018 | | | 985 | |
1019 | if (prog_ioctl(s, SIOCGIFPREFIX_IN6, ipr) < 0) { | | 986 | prefix->rainfo = rai; |
1020 | logit(LOG_INFO, "<%s> ioctl:SIOCGIFPREFIX: %m", __func__); | | 987 | TAILQ_INSERT_TAIL(&rai->prefix, prefix, next); |
| | | 988 | rai->pfxs++; |
1021 | | | 989 | |
1022 | ipr->ipr_vltime = DEF_ADVVALIDLIFETIME; | | 990 | logit(LOG_DEBUG, "<%s> new prefix %s/%d was added on %s", |
1023 | ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME; | | 991 | __func__, inet_ntop(AF_INET6, addr, ntopbuf, INET6_ADDRSTRLEN), |
1024 | ipr->ipr_raf_onlink = 1; | | 992 | plen, rai->ifname); |
1025 | ipr->ipr_raf_auto = 1; | | | |
1026 | /* omit other field initialization */ | | | |
1027 | } | | | |
1028 | else if (ipr->ipr_origin < PR_ORIG_RR) { | | | |
1029 | char ntopbuf[INET6_ADDRSTRLEN]; | | | |
1030 | | | | |
1031 | logit(LOG_WARNING, "<%s> Added prefix(%s)'s origin %d is" | | | |
1032 | "lower than PR_ORIG_RR(router renumbering)." | | | |
1033 | "This should not happen if I am router", __func__, | | | |
1034 | inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr, ntopbuf, | | | |
1035 | sizeof(ntopbuf)), ipr->ipr_origin); | | | |
1036 | prog_close(s); | | | |
1037 | return 1; | | | |
1038 | } | | | |
1039 | | | | |
1040 | prog_close(s); | | | |
1041 | return 0; | | | |
1042 | #else | | | |
1043 | ipr->ipr_vltime = DEF_ADVVALIDLIFETIME; | | | |
1044 | ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME; | | | |
1045 | ipr->ipr_raf_onlink = 1; | | | |
1046 | ipr->ipr_raf_auto = 1; | | | |
1047 | return 0; | | | |
1048 | #endif | | | |
1049 | } | | | |
1050 | | | 993 | |
1051 | void | | 994 | /* free the previous packet */ |
1052 | make_prefix(struct rainfo *rai, int ifindex, struct in6_addr *addr, int plen) | | 995 | free(rai->ra_data); |
1053 | { | | 996 | rai->ra_data = NULL; |
1054 | struct in6_prefixreq ipr; | | | |
1055 | | | 997 | |
1056 | memset(&ipr, 0, sizeof(ipr)); | | 998 | /* reconstruct the packet */ |
1057 | if (if_indextoname(ifindex, ipr.ipr_name) == NULL) { | | 999 | make_packet(rai); |
1058 | logit(LOG_ERR, "<%s> Prefix added interface No.%d doesn't" | | | |
1059 | "exist. This should not happen: %m", __func__, | | | |
1060 | ifindex); | | | |
1061 | exit(1); | | | |
1062 | } | | | |
1063 | ipr.ipr_prefix.sin6_len = sizeof(ipr.ipr_prefix); | | | |
1064 | ipr.ipr_prefix.sin6_family = AF_INET6; | | | |
1065 | ipr.ipr_prefix.sin6_addr = *addr; | | | |
1066 | ipr.ipr_plen = plen; | | | |
1067 | | | | |
1068 | if (init_prefix(&ipr)) | | | |
1069 | return; /* init failed by some error */ | | | |
1070 | add_prefix(rai, &ipr); | | | |
1071 | } | | 1000 | } |
1072 | | | 1001 | |
1073 | void | | 1002 | void |
1074 | make_packet(struct rainfo *rainfo) | | 1003 | make_packet(struct rainfo *rainfo) |
1075 | { | | 1004 | { |
1076 | size_t packlen, lladdroptlen = 0; | | 1005 | size_t packlen, lladdroptlen = 0; |
1077 | char *buf; | | 1006 | char *buf; |
1078 | struct nd_router_advert *ra; | | 1007 | struct nd_router_advert *ra; |
1079 | struct nd_opt_prefix_info *ndopt_pi; | | 1008 | struct nd_opt_prefix_info *ndopt_pi; |
1080 | struct nd_opt_mtu *ndopt_mtu; | | 1009 | struct nd_opt_mtu *ndopt_mtu; |
1081 | struct prefix *pfx; | | 1010 | struct prefix *pfx; |
1082 | struct nd_opt_route_info *ndopt_rti; | | 1011 | struct nd_opt_route_info *ndopt_rti; |
1083 | struct rtinfo *rti; | | 1012 | struct rtinfo *rti; |