| 1 | /* SCTP kernel Implementation: User API extensions. |
| 2 | * |
| 3 | * connectx.c |
| 4 | * |
| 5 | * Distributed under the terms of the LGPL v2.1 as described in |
| 6 | * http://www.gnu.org/copyleft/lesser.txt. |
| 7 | * |
| 8 | * This file is part of the user library that offers support for the |
| 9 | * SCTP kernel Implementation. The main purpose of this |
| 10 | * code is to provide the SCTP Socket API mappings for user |
| 11 | * application to interface with the SCTP in kernel. |
| 12 | * |
| 13 | * This implementation is based on the Socket API Extensions for SCTP |
| 14 | * defined in <draft-ietf-tsvwg-sctpsocket-10.txt. |
| 15 | * |
| 16 | * (C) Copyright IBM Corp. 2001, 2005 |
| 17 | * |
| 18 | * Written or modified by: |
| 19 | * Frank Filz <[email protected]> |
| 20 | */ |
| 21 | |
| 22 | #include <sys/socket.h> /* struct sockaddr_storage, setsockopt() */ |
| 23 | #include <netinet/in.h> |
| 24 | #include <netinet/sctp.h>cxx . . $ `<file>/src/include/netinet/sctp.h`/ /* SCTP_SOCKOPT_CONNECTX_* */ |
| 25 | #include <errno.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <fcntl.h> |
| 29 | #include "config.h"cxx . . $ `<file>/config.h`/ |
| 30 | |
| 31 | #define __SYMPFXcxx . . $ `src/lib/connectx.c:31:9`!(pfx, sym) #pfx sym |
| 32 | #define _SYMPFXcxx . . $ `src/lib/connectx.c:32:9`!(pfx, sym) __SYMPFXcxx . . $ `src/lib/connectx.c:31:9`!(pfx, sym) |
| 33 | #define SYMPFXcxx . . $ `src/lib/connectx.c:33:9`!(sym) _SYMPFXcxx . . $ `src/lib/connectx.c:32:9`!(__USER_LABEL_PREFIX__cxx . . $ `<built-in>:334:9`!, #sym) |
| 34 | |
| 35 | #if HAVE_ATTRIBUTE_SYMVERcxx . . $ `config.h:8:9`! |
| 36 | #define SYMVERcxx . . $ `src/lib/connectx.c:36:9`!(name, name2) __attribute__ ((symver (SYMPFXcxx . . $ `src/lib/connectx.c:33:9`!(name2)))) |
| 37 | #else |
| 38 | #define SYMVER(name, name2) __asm__(".symver " SYMPFX(name) "," SYMPFX(name2)); |
| 39 | #endif |
| 40 | |
| 41 | |
| 42 | /* Support the sctp_connectx() interface. |
| 43 | * |
| 44 | * See Sockets API Extensions for SCTP. Section 8.1. |
| 45 | * |
| 46 | * Instead of implementing through a socket call in sys_socketcall(), |
| 47 | * tunnel the request through setsockopt(). |
| 48 | */ |
| 49 | static int __connectx_addrsizecxx . . $ __connectx_addrsize(cf301243b827752c).(const struct sockaddrcxx . . $ sockaddr# *addrs, |
| 50 | const int addrcnt) |
| 51 | { |
| 52 | const char *addrbuf; |
| 53 | const struct sockaddrcxx . . $ sockaddr# *sa_addr; |
| 54 | int addrs_size = 0; |
| 55 | int i; |
| 56 | |
| 57 | addrbuf = (char *)addrs; |
| 58 | for (i = 0; i < addrcnt; i++) { |
| 59 | sa_addr = (const struct sockaddrcxx . . $ sockaddr# *)addrbuf; |
| 60 | switch (sa_addr->sa_familycxx . . $ sockaddr#sa_family.) { |
| 61 | case AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!: |
| 62 | addrs_size += sizeof(struct sockaddr_incxx . . $ sockaddr_in#); |
| 63 | addrbuf += sizeof(struct sockaddr_incxx . . $ sockaddr_in#); |
| 64 | break; |
| 65 | case AF_INET6cxx . . $ `<external>/8962547315054503424/socket.h:105:9`!: |
| 66 | addrs_size += sizeof(struct sockaddr_in6cxx . . $ sockaddr_in6#); |
| 67 | addrbuf += sizeof(struct sockaddr_in6cxx . . $ sockaddr_in6#); |
| 68 | break; |
| 69 | default: |
| 70 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 71 | return -1; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return addrs_size; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | SYMVERcxx . . $ `src/lib/connectx.c:36:9`!(__sctp_connectx, sctp_connectx@) |
| 80 | int __sctp_connectxcxx . . $ __sctp_connectx(35e267560d28d16b).(int fd, struct sockaddrcxx . . $ sockaddr# *addrs, int addrcnt) |
| 81 | { |
| 82 | int addrs_size = __connectx_addrsizecxx . . $ __connectx_addrsize(cf301243b827752c).(addrs, addrcnt); |
| 83 | |
| 84 | if (addrs_size < 0) |
| 85 | return addrs_size; |
| 86 | |
| 87 | return setsockoptcxx . . $ setsockopt(506cc1d0fc70fdf1).(fd, SOL_SCTPcxx . . $ `src/include/netinet/sctp.h:48:9`!, SCTP_SOCKOPT_CONNECTX_OLDcxx . . $ `<external>/17169092326581286071/sctp.h:117:9`!, addrs, |
| 88 | addrs_size); |
| 89 | } |
| 90 | |
| 91 | SYMVERcxx . . $ `src/lib/connectx.c:36:9`!(sctp_connectx_orig, sctp_connectx@VERS_1) |
| 92 | extern int sctp_connectx_origcxx . . $ sctp_connectx_orig(7864480464b09eea). (int) |
| 93 | __attribute ((alias ("__sctp_connectx"))); |
| 94 | |
| 95 | |
| 96 | static int __connectxcxx . . $ __connectx(791612adedea70b2).(int fd, struct sockaddrcxx . . $ sockaddr# *addrs, socklen_tcxx . . $ socklen_t# addrs_size, |
| 97 | sctp_assoc_tcxx . . $ sctp_assoc_t# *id) |
| 98 | { |
| 99 | int status; |
| 100 | |
| 101 | if (id) |
| 102 | *id = 0; |
| 103 | |
| 104 | status = setsockoptcxx . . $ setsockopt(506cc1d0fc70fdf1).(fd, SOL_SCTPcxx . . $ `src/include/netinet/sctp.h:48:9`!, SCTP_SOCKOPT_CONNECTXcxx . . $ `<external>/17169092326581286071/sctp.h:120:9`!, addrs, |
| 105 | addrs_size); |
| 106 | |
| 107 | /* Normalize status and set association id */ |
| 108 | if (status > 0) { |
| 109 | if (id) |
| 110 | *id = status; |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | /* The error is something other then "Option not supported" */ |
| 115 | if (status < 0 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != ENOPROTOOPTcxx . . $ `<external>/17922322188510340118/errno.h:76:9`!) |
| 116 | return status; |
| 117 | |
| 118 | /* At this point, if the application wanted the id, we can't |
| 119 | * really provide it, so we can return ENOPROTOOPT. |
| 120 | */ |
| 121 | if (id) { |
| 122 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = ENOPROTOOPTcxx . . $ `<external>/17922322188510340118/errno.h:76:9`!; |
| 123 | return -1; |
| 124 | } |
| 125 | |
| 126 | /* Finally, try the old API */ |
| 127 | return setsockoptcxx . . $ setsockopt(506cc1d0fc70fdf1).(fd, SOL_SCTPcxx . . $ `src/include/netinet/sctp.h:48:9`!, SCTP_SOCKOPT_CONNECTX_OLDcxx . . $ `<external>/17169092326581286071/sctp.h:117:9`!, |
| 128 | addrs, addrs_size); |
| 129 | } |
| 130 | |
| 131 | SYMVERcxx . . $ `src/lib/connectx.c:36:9`!(sctp_connectx2, sctp_connectx@VERS_2) |
| 132 | int sctp_connectx2cxx . . $ sctp_connectx2(c1090d8010e18ca3).(int fd, struct sockaddrcxx . . $ sockaddr# *addrs, int addrcnt, |
| 133 | sctp_assoc_tcxx . . $ sctp_assoc_t# *id) |
| 134 | { |
| 135 | int addrs_size = __connectx_addrsizecxx . . $ __connectx_addrsize(cf301243b827752c).(addrs, addrcnt); |
| 136 | |
| 137 | if (addrs_size < 0) |
| 138 | return addrs_size; |
| 139 | |
| 140 | return __connectxcxx . . $ __connectx(791612adedea70b2).(fd, addrs, addrs_size, id); |
| 141 | } |
| 142 | |
| 143 | SYMVERcxx . . $ `src/lib/connectx.c:36:9`!(sctp_connectx3, sctp_connectx@@VERS_3) |
| 144 | int sctp_connectx3cxx . . $ sctp_connectx3(c1090d8010e18ca3).(int fd, struct sockaddrcxx . . $ sockaddr# *addrs, int addrcnt, |
| 145 | sctp_assoc_tcxx . . $ sctp_assoc_t# *id) |
| 146 | { |
| 147 | int addrs_size = __connectx_addrsizecxx . . $ __connectx_addrsize(cf301243b827752c).(addrs, addrcnt); |
| 148 | int status; |
| 149 | struct sctp_getaddrs_oldcxx . . $ sctp_getaddrs_old# param; |
| 150 | socklen_tcxx . . $ socklen_t# opt_len = sizeof(param); |
| 151 | |
| 152 | if (addrs_size < 0) |
| 153 | return addrs_size; |
| 154 | |
| 155 | /* First try the new socket api |
| 156 | * Because the id is returned in the option buffer we have prepend |
| 157 | * 32bit to it for the returned association id |
| 158 | */ |
| 159 | param.assoc_idcxx . . $ sctp_getaddrs_old#assoc_id. = 0; |
| 160 | param.addr_numcxx . . $ sctp_getaddrs_old#addr_num. = addrs_size; |
| 161 | param.addrscxx . . $ sctp_getaddrs_old#addrs. = addrs; |
| 162 | status = getsockoptcxx . . $ getsockopt(b79d9f11efef15f9).(fd, SOL_SCTPcxx . . $ `src/include/netinet/sctp.h:48:9`!, SCTP_SOCKOPT_CONNECTX3cxx . . $ `<external>/17169092326581286071/sctp.h:121:9`!, |
| 163 | ¶m, &opt_len); |
| 164 | if (status == 0 || errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EINPROGRESScxx . . $ `<external>/17922322188510340118/errno.h:99:9`!) { |
| 165 | /* Succeeded immediately, or initiated on non-blocking |
| 166 | * socket. |
| 167 | */ |
| 168 | if (id) |
| 169 | *id = param.assoc_idcxx . . $ sctp_getaddrs_old#assoc_id.; |
| 170 | } |
| 171 | |
| 172 | if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != ENOPROTOOPTcxx . . $ `<external>/17922322188510340118/errno.h:76:9`!) { |
| 173 | /* No point in trying the fallbacks*/ |
| 174 | return status; |
| 175 | } |
| 176 | |
| 177 | /* The first incarnation of updated connectx api didn't work for |
| 178 | * non-blocking sockets. So if the application wants the association |
| 179 | * id and the socket is non-blocking, we can't really do anything. |
| 180 | */ |
| 181 | if (id) { |
| 182 | /* Program wants the association-id returned. We can only do |
| 183 | * that if the socket is blocking */ |
| 184 | status = fcntlcxx . . $ fcntl(cac4efb70f20a0fb).(fd, F_GETFLcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:168:9`!); |
| 185 | if (status < 0) |
| 186 | return status; |
| 187 | |
| 188 | if (status & O_NONBLOCKcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:62:10`!) { |
| 189 | /* Socket is non-blocking. Fail */ |
| 190 | errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = ENOPROTOOPTcxx . . $ `<external>/17922322188510340118/errno.h:76:9`!; |
| 191 | return -1; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | return __connectxcxx . . $ __connectx(791612adedea70b2).(fd, addrs, addrs_size, id); |
| 196 | } |
| 197 |