[package] busybox: Comcast uses option 150 for 6RD DHCP announcements, cover it
[openwrt.git] / package / busybox / patches / 244-udhcpc_add_6rd_option.patch
1 --- a/networking/udhcp/common.c
2 +++ b/networking/udhcp/common.c
3 @@ -54,6 +54,8 @@ const struct dhcp_optflag dhcp_optflags[
4         { OPTION_SIP_SERVERS                      , 0x78 }, /* DHCP_SIP_SERVERS   */
5  #endif
6         { OPTION_STATIC_ROUTES                    , 0x79 }, /* DHCP_STATIC_ROUTES */
7 +       { OPTION_6RD                              , 0xd4 }, /* DHCP_6RD (RFC)     */
8 +       { OPTION_6RD                              , 0x96 }, /* DHCP_6RD (Comcast) */
9         { OPTION_STRING                           , 0xfc }, /* DHCP_WPAD          */
10  
11         /* Options below have no match in dhcp_option_strings[],
12 @@ -114,6 +116,8 @@ const char dhcp_option_strings[] ALIGN1 
13  // doesn't work in udhcpd.conf since OPTION_STATIC_ROUTES
14  // is not handled yet by "string->option" conversion code:
15         "staticroutes" "\0"/* DHCP_STATIC_ROUTES  */
16 +       "ip6rd" "\0"       /* DHCP_6RD (RFC)      */
17 +       "ip6rd" "\0"       /* DHCP_6RD (Comcast)  */
18         "wpad" "\0"        /* DHCP_WPAD           */
19         ;
20  
21 @@ -141,6 +145,7 @@ const uint8_t dhcp_option_lengths[] ALIG
22         [OPTION_S32] =     4,
23         /* Just like OPTION_STRING, we use minimum length here */
24         [OPTION_STATIC_ROUTES] = 5,
25 +       [OPTION_6RD]           = 22,
26  };
27  
28  
29 --- a/networking/udhcp/dhcpc.c
30 +++ b/networking/udhcp/dhcpc.c
31 @@ -45,6 +45,7 @@ static const uint8_t len_of_option_as_st
32         [OPTION_IP              ] = sizeof("255.255.255.255 "),
33         [OPTION_IP_PAIR         ] = sizeof("255.255.255.255 ") * 2,
34         [OPTION_STATIC_ROUTES   ] = sizeof("255.255.255.255/32 255.255.255.255 "),
35 +       [OPTION_6RD             ] = sizeof("32 128 FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF 255.255.255.255 "),
36         [OPTION_STRING          ] = 1,
37  #if ENABLE_FEATURE_UDHCP_RFC3397
38         [OPTION_DNS_STRING      ] = 1, /* unused */
39 @@ -68,6 +69,23 @@ static int sprint_nip(char *dest, const 
40         return sprintf(dest, "%s%u.%u.%u.%u", pre, ip[0], ip[1], ip[2], ip[3]);
41  }
42  
43 +static int sprint_nip6(char *dest, const char *pre, const uint8_t *ip)
44 +{
45 +       int len = 0;
46 +       int off;
47 +       uint16_t word;
48 +
49 +       len += sprintf(dest, "%s", pre);
50 +
51 +       for (off = 0; off < 16; off += 2)
52 +       {
53 +               move_from_unaligned16(word, &ip[off]);
54 +               len += sprintf(dest+len, "%s%04X", off ? ":" : "", htons(word));
55 +       }
56 +
57 +       return len;
58 +}
59 +
60  /* really simple implementation, just count the bits */
61  static int mton(uint32_t mask)
62  {
63 @@ -177,6 +195,70 @@ static NOINLINE char *xmalloc_optname_op
64  
65                         return ret;
66                 }
67 +               case OPTION_6RD: {
68 +                       /* Option binary format:
69 +                        *  0                   1                   2                   3
70 +                        *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
71 +                        *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
72 +                        *  |  OPTION_6RD   | option-length |  IPv4MaskLen  |  6rdPrefixLen |
73 +                        *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
74 +                        *  |                                                               |
75 +                        *  |                           6rdPrefix                           |
76 +                        *  |                          (16 octets)                          |
77 +                        *  |                                                               |
78 +                        *  |                                                               |
79 +                        *  |                                                               |
80 +                        *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
81 +                        *  |                     6rdBRIPv4Address(es)                      |
82 +                        *  .                                                               .
83 +                        *  .                                                               .
84 +                        *  .                                                               .
85 +                        *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
86 +                        *
87 +                        * We convert it to a string "IPv4MaskLen 6rdPrefixLen 6rdPrefix 6rdBRIPv4Address"
88 +                        */
89 +
90 +                       /* Sanity check: ensure that our length is at least 22 bytes, that
91 +                        * IPv4MaskLen is <= 32, 6rdPrefixLen <= 128 and that the sum of
92 +                        * (32 - IPv4MaskLen) + 6rdPrefixLen is less than or equal to 128.
93 +                        * If any of these requirements is not fulfilled, return with empty
94 +                        * value.
95 +                        */
96 +                       if ((len >= 22) && (*option <= 32) && (*(option+1) <= 128) &&
97 +                           (((32 - *option) + *(option+1)) <= 128))
98 +                       {
99 +                               /* IPv4MaskLen */
100 +                               dest += sprintf(dest, "%u ", *option++);
101 +                               len--;
102 +
103 +                               /* 6rdPrefixLen */
104 +                               dest += sprintf(dest, "%u ", *option++);
105 +                               len--;
106 +
107 +                               /* 6rdPrefix */
108 +                               dest += sprint_nip6(dest, "", option);
109 +                               option += 16;
110 +                               len -= 16;
111 +
112 +                               /* 6rdBRIPv4Addresses */
113 +                               while (len >= 4)
114 +                               {
115 +                                       dest += sprint_nip(dest, " ", option);
116 +                                       option += 4;
117 +                                       len -= 4;
118 +
119 +                                       /* the code to determine the option size fails to work with
120 +                                        * lengths that are not a multiple of the minimum length,
121 +                                        * adding all advertised 6rdBRIPv4Addresses here would
122 +                                        * overflow the destination buffer, therefore skip the rest
123 +                                        * for now
124 +                                        */
125 +                                       break;
126 +                               }
127 +                       }
128 +
129 +                       return ret;
130 +               }
131  #if ENABLE_FEATURE_UDHCP_RFC3397
132                 case OPTION_DNS_STRING:
133                         /* unpack option into dest; use ret for prefix (i.e., "optname=") */
134 --- a/networking/udhcp/common.h
135 +++ b/networking/udhcp/common.h
136 @@ -88,6 +88,7 @@ enum {
137         OPTION_S32,
138         OPTION_BIN,
139         OPTION_STATIC_ROUTES,
140 +       OPTION_6RD,
141  #if ENABLE_FEATURE_UDHCP_RFC3397
142         OPTION_DNS_STRING,  /* RFC1035 compressed domain name list */
143         OPTION_SIP_SERVERS,