There are a number of instances throughout the code where the constant 28 is
authorMatt Caswell <matt@openssl.org>
Mon, 1 Dec 2014 23:49:47 +0000 (23:49 +0000)
committerMatt Caswell <matt@openssl.org>
Wed, 3 Dec 2014 09:41:16 +0000 (09:41 +0000)
used with no explanation. Some of this was introduced as part of RT#1929. The
value 28 is the length of the IP header (20 bytes) plus the UDP header (8
bytes). However use of this constant is incorrect because there may be
instances where a different value is needed, e.g. an IPv4 header is 20 bytes
but an IPv6 header is 40. Similarly you may not be using UDP (e.g. SCTP).
This commit introduces a new BIO_CTRL that provides the value to be used for
this mtu "overhead". It will be used by subsequent commits.

Reviewed-by: Tim Hudson <tjh@openssl.org>
(cherry picked from commit 0d3ae34df573f477b6b1aaf614d52dcdfcff5fce)

Conflicts:
crypto/bio/bio.h
crypto/bio/bss_dgram.c

crypto/bio/bio.h
crypto/bio/bss_dgram.c

index 3c39d1844d4f657bde892abc1389476a5203d4d7..c67f3a1baa76618466bd77c1aeb669e286bacd7d 100644 (file)
@@ -162,7 +162,8 @@ extern "C" {
 #define BIO_CTRL_DGRAM_SET_PEER           44 /* Destination for the data */
 
 #define BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT   45 /* Next DTLS handshake timeout to
-                                                                                         * adjust socket timeouts */
+                                              * adjust socket timeouts */
+#define BIO_CTRL_DGRAM_GET_MTU_OVERHEAD   49
 
 /* modifiers */
 #define BIO_FP_READ            0x02
@@ -553,6 +554,8 @@ int BIO_ctrl_reset_read_request(BIO *b);
          (int)BIO_ctrl(b, BIO_CTRL_DGRAM_GET_PEER, 0, (char *)peer)
 #define BIO_dgram_set_peer(b,peer) \
          (int)BIO_ctrl(b, BIO_CTRL_DGRAM_SET_PEER, 0, (char *)peer)
+#define BIO_dgram_get_mtu_overhead(b) \
+         (unsigned int)BIO_ctrl((b), BIO_CTRL_DGRAM_GET_MTU_OVERHEAD, 0, NULL)
 
 /* These two aren't currently implemented */
 /* int BIO_get_ex_num(BIO *bio); */
index a223b8367cb565b509cf3d4f10f587c81227d184..cbf9565d6d99dd43582474de41645f051a519bca 100644 (file)
@@ -375,6 +375,36 @@ static int dgram_write(BIO *b, const char *in, int inl)
        return(ret);
        }
 
+static long dgram_get_mtu_overhead(bio_dgram_data *data)
+       {
+       long ret;
+
+       switch (data->peer.sa.sa_family)
+               {
+               case AF_INET:
+                       /* Assume this is UDP - 20 bytes for IP, 8 bytes for UDP */
+                       ret = 28;
+                       break;
+#if OPENSSL_USE_IPV6
+               case AF_INET6:
+#ifdef IN6_IS_ADDR_V4MAPPED
+                       if (IN6_IS_ADDR_V4MAPPED(&data->peer.sa_in6.sin6_addr))
+                               /* Assume this is UDP - 20 bytes for IP, 8 bytes for UDP */
+                               ret = 28;
+                       else
+#endif
+                               /* Assume this is UDP - 40 bytes for IP, 8 bytes for UDP */
+                               ret = 48;
+                       break;
+#endif
+               default:
+                       /* We don't know. Go with the historical default */
+                       ret = 28;
+                       break;
+               }
+       return ret;
+       }
+
 static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
        {
        long ret=1;
@@ -551,23 +581,24 @@ static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
 #endif
                break;
        case BIO_CTRL_DGRAM_GET_FALLBACK_MTU:
+               ret = -dgram_get_mtu_overhead(data);
                switch (data->peer.sa.sa_family)
                        {
                        case AF_INET:
-                               ret = 576 - 20 - 8;
+                               ret += 576;
                                break;
 #if OPENSSL_USE_IPV6
                        case AF_INET6:
 #ifdef IN6_IS_ADDR_V4MAPPED
                                if (IN6_IS_ADDR_V4MAPPED(&data->peer.sa_in6.sin6_addr))
-                                       ret = 576 - 20 - 8;
+                                       ret += 576;
                                else
 #endif
-                                       ret = 1280 - 40 - 8;
+                                       ret += 1280;
                                break;
 #endif
                        default:
-                               ret = 576 - 20 - 8;
+                               ret += 576;
                                break;
                        }
                break;
@@ -768,6 +799,9 @@ static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
                        ret = 0;
                break;
 #endif
+       case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
+               ret = dgram_get_mtu_overhead(data);
+               break;
        default:
                ret=0;
                break;