Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * INET An implementation of the TCP/IP protocol suite for the LINUX |
| 3 | * operating system. INET is implemented using the BSD Socket |
| 4 | * interface as the means of communication with the user level. |
| 5 | * |
| 6 | * The IP to API glue. |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 7 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * Authors: see ip.c |
| 9 | * |
| 10 | * Fixes: |
| 11 | * Many : Split from ip.c , see ip.c for history. |
| 12 | * Martin Mares : TOS setting fixed. |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 13 | * Alan Cox : Fixed a couple of oopses in Martin's |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * TOS tweaks. |
| 15 | * Mike McLagan : Routing by source |
| 16 | */ |
| 17 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/module.h> |
| 19 | #include <linux/types.h> |
| 20 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/skbuff.h> |
| 22 | #include <linux/ip.h> |
| 23 | #include <linux/icmp.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 24 | #include <linux/inetdevice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/netdevice.h> |
| 26 | #include <net/sock.h> |
| 27 | #include <net/ip.h> |
| 28 | #include <net/icmp.h> |
Arnaldo Carvalho de Melo | d83d846 | 2005-12-13 23:26:10 -0800 | [diff] [blame] | 29 | #include <net/tcp_states.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/udp.h> |
| 31 | #include <linux/igmp.h> |
| 32 | #include <linux/netfilter.h> |
| 33 | #include <linux/route.h> |
| 34 | #include <linux/mroute.h> |
| 35 | #include <net/route.h> |
| 36 | #include <net/xfrm.h> |
David L Stevens | dae5029 | 2008-04-27 01:06:07 -0700 | [diff] [blame] | 37 | #include <net/compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
| 39 | #include <net/transp_v6.h> |
| 40 | #endif |
| 41 | |
| 42 | #include <linux/errqueue.h> |
| 43 | #include <asm/uaccess.h> |
| 44 | |
| 45 | #define IP_CMSG_PKTINFO 1 |
| 46 | #define IP_CMSG_TTL 2 |
| 47 | #define IP_CMSG_TOS 4 |
| 48 | #define IP_CMSG_RECVOPTS 8 |
| 49 | #define IP_CMSG_RETOPTS 16 |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 50 | #define IP_CMSG_PASSSEC 32 |
Balazs Scheidler | e8b2dfe | 2008-11-16 19:32:39 -0800 | [diff] [blame] | 51 | #define IP_CMSG_ORIGDSTADDR 64 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 53 | /* |
| 54 | * SOL_IP control messages. |
| 55 | */ |
| 56 | |
| 57 | static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb) |
| 58 | { |
| 59 | struct in_pktinfo info; |
Eric Dumazet | 511c3f9 | 2009-06-02 05:14:27 +0000 | [diff] [blame] | 60 | struct rtable *rt = skb_rtable(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 62 | info.ipi_addr.s_addr = ip_hdr(skb)->daddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | if (rt) { |
| 64 | info.ipi_ifindex = rt->rt_iif; |
| 65 | info.ipi_spec_dst.s_addr = rt->rt_spec_dst; |
| 66 | } else { |
| 67 | info.ipi_ifindex = 0; |
| 68 | info.ipi_spec_dst.s_addr = 0; |
| 69 | } |
| 70 | |
| 71 | put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info); |
| 72 | } |
| 73 | |
| 74 | static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb) |
| 75 | { |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 76 | int ttl = ip_hdr(skb)->ttl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl); |
| 78 | } |
| 79 | |
| 80 | static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb) |
| 81 | { |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 82 | put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb) |
| 86 | { |
| 87 | if (IPCB(skb)->opt.optlen == 0) |
| 88 | return; |
| 89 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 90 | put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen, |
| 91 | ip_hdr(skb) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | |
| 95 | static void ip_cmsg_recv_retopts(struct msghdr *msg, struct sk_buff *skb) |
| 96 | { |
| 97 | unsigned char optbuf[sizeof(struct ip_options) + 40]; |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 98 | struct ip_options * opt = (struct ip_options *)optbuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
| 100 | if (IPCB(skb)->opt.optlen == 0) |
| 101 | return; |
| 102 | |
| 103 | if (ip_options_echo(opt, skb)) { |
| 104 | msg->msg_flags |= MSG_CTRUNC; |
| 105 | return; |
| 106 | } |
| 107 | ip_options_undo(opt); |
| 108 | |
| 109 | put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data); |
| 110 | } |
| 111 | |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 112 | static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb) |
| 113 | { |
| 114 | char *secdata; |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 115 | u32 seclen, secid; |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 116 | int err; |
| 117 | |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 118 | err = security_socket_getpeersec_dgram(NULL, skb, &secid); |
| 119 | if (err) |
| 120 | return; |
| 121 | |
| 122 | err = security_secid_to_secctx(secid, &secdata, &seclen); |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 123 | if (err) |
| 124 | return; |
| 125 | |
| 126 | put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata); |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 127 | security_release_secctx(secdata, seclen); |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Harvey Harrison | 21d1a16 | 2008-11-20 01:54:27 -0800 | [diff] [blame] | 130 | static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb) |
Balazs Scheidler | e8b2dfe | 2008-11-16 19:32:39 -0800 | [diff] [blame] | 131 | { |
| 132 | struct sockaddr_in sin; |
| 133 | struct iphdr *iph = ip_hdr(skb); |
Harvey Harrison | 21d1a16 | 2008-11-20 01:54:27 -0800 | [diff] [blame] | 134 | __be16 *ports = (__be16 *)skb_transport_header(skb); |
Balazs Scheidler | e8b2dfe | 2008-11-16 19:32:39 -0800 | [diff] [blame] | 135 | |
| 136 | if (skb_transport_offset(skb) + 4 > skb->len) |
| 137 | return; |
| 138 | |
| 139 | /* All current transport protocols have the port numbers in the |
| 140 | * first four bytes of the transport header and this function is |
| 141 | * written with this assumption in mind. |
| 142 | */ |
| 143 | |
| 144 | sin.sin_family = AF_INET; |
| 145 | sin.sin_addr.s_addr = iph->daddr; |
| 146 | sin.sin_port = ports[1]; |
| 147 | memset(sin.sin_zero, 0, sizeof(sin.sin_zero)); |
| 148 | |
| 149 | put_cmsg(msg, SOL_IP, IP_ORIGDSTADDR, sizeof(sin), &sin); |
| 150 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
| 152 | void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb) |
| 153 | { |
| 154 | struct inet_sock *inet = inet_sk(skb->sk); |
| 155 | unsigned flags = inet->cmsg_flags; |
| 156 | |
| 157 | /* Ordered by supposed usage frequency */ |
| 158 | if (flags & 1) |
| 159 | ip_cmsg_recv_pktinfo(msg, skb); |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 160 | if ((flags >>= 1) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | return; |
| 162 | |
| 163 | if (flags & 1) |
| 164 | ip_cmsg_recv_ttl(msg, skb); |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 165 | if ((flags >>= 1) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | return; |
| 167 | |
| 168 | if (flags & 1) |
| 169 | ip_cmsg_recv_tos(msg, skb); |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 170 | if ((flags >>= 1) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | return; |
| 172 | |
| 173 | if (flags & 1) |
| 174 | ip_cmsg_recv_opts(msg, skb); |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 175 | if ((flags >>= 1) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | return; |
| 177 | |
| 178 | if (flags & 1) |
| 179 | ip_cmsg_recv_retopts(msg, skb); |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 180 | if ((flags >>= 1) == 0) |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 181 | return; |
| 182 | |
| 183 | if (flags & 1) |
| 184 | ip_cmsg_recv_security(msg, skb); |
Balazs Scheidler | e8b2dfe | 2008-11-16 19:32:39 -0800 | [diff] [blame] | 185 | |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 186 | if ((flags >>= 1) == 0) |
Balazs Scheidler | e8b2dfe | 2008-11-16 19:32:39 -0800 | [diff] [blame] | 187 | return; |
| 188 | if (flags & 1) |
| 189 | ip_cmsg_recv_dstaddr(msg, skb); |
| 190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | } |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 192 | EXPORT_SYMBOL(ip_cmsg_recv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
Denis V. Lunev | 7a6adb9 | 2008-03-24 15:30:27 -0700 | [diff] [blame] | 194 | int ip_cmsg_send(struct net *net, struct msghdr *msg, struct ipcm_cookie *ipc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | { |
| 196 | int err; |
| 197 | struct cmsghdr *cmsg; |
| 198 | |
| 199 | for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) { |
| 200 | if (!CMSG_OK(msg, cmsg)) |
| 201 | return -EINVAL; |
| 202 | if (cmsg->cmsg_level != SOL_IP) |
| 203 | continue; |
| 204 | switch (cmsg->cmsg_type) { |
| 205 | case IP_RETOPTS: |
| 206 | err = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr)); |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 207 | err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg), |
| 208 | err < 40 ? err : 40); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | if (err) |
| 210 | return err; |
| 211 | break; |
| 212 | case IP_PKTINFO: |
| 213 | { |
| 214 | struct in_pktinfo *info; |
| 215 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo))) |
| 216 | return -EINVAL; |
| 217 | info = (struct in_pktinfo *)CMSG_DATA(cmsg); |
| 218 | ipc->oif = info->ipi_ifindex; |
| 219 | ipc->addr = info->ipi_spec_dst.s_addr; |
| 220 | break; |
| 221 | } |
| 222 | default: |
| 223 | return -EINVAL; |
| 224 | } |
| 225 | } |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | |
| 230 | /* Special input handler for packets caught by router alert option. |
| 231 | They are selected only by protocol field, and then processed likely |
| 232 | local ones; but only if someone wants them! Otherwise, router |
| 233 | not running rsvpd will kill RSVP. |
| 234 | |
| 235 | It is user level problem, what it will make with them. |
| 236 | I have no idea, how it will masquearde or NAT them (it is joke, joke :-)), |
| 237 | but receiver should be enough clever f.e. to forward mtrace requests, |
| 238 | sent to multicast group to reach destination designated router. |
| 239 | */ |
| 240 | struct ip_ra_chain *ip_ra_chain; |
| 241 | DEFINE_RWLOCK(ip_ra_lock); |
| 242 | |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 243 | int ip_ra_control(struct sock *sk, unsigned char on, |
| 244 | void (*destructor)(struct sock *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | { |
| 246 | struct ip_ra_chain *ra, *new_ra, **rap; |
| 247 | |
Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 248 | if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | return -EINVAL; |
| 250 | |
| 251 | new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL; |
| 252 | |
| 253 | write_lock_bh(&ip_ra_lock); |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 254 | for (rap = &ip_ra_chain; (ra = *rap) != NULL; rap = &ra->next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | if (ra->sk == sk) { |
| 256 | if (on) { |
| 257 | write_unlock_bh(&ip_ra_lock); |
Jesper Juhl | a51482b | 2005-11-08 09:41:34 -0800 | [diff] [blame] | 258 | kfree(new_ra); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | return -EADDRINUSE; |
| 260 | } |
| 261 | *rap = ra->next; |
| 262 | write_unlock_bh(&ip_ra_lock); |
| 263 | |
| 264 | if (ra->destructor) |
| 265 | ra->destructor(sk); |
| 266 | sock_put(sk); |
| 267 | kfree(ra); |
| 268 | return 0; |
| 269 | } |
| 270 | } |
| 271 | if (new_ra == NULL) { |
| 272 | write_unlock_bh(&ip_ra_lock); |
| 273 | return -ENOBUFS; |
| 274 | } |
| 275 | new_ra->sk = sk; |
| 276 | new_ra->destructor = destructor; |
| 277 | |
| 278 | new_ra->next = ra; |
| 279 | *rap = new_ra; |
| 280 | sock_hold(sk); |
| 281 | write_unlock_bh(&ip_ra_lock); |
| 282 | |
| 283 | return 0; |
| 284 | } |
| 285 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 286 | void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err, |
Al Viro | 35986b3 | 2006-09-27 18:34:21 -0700 | [diff] [blame] | 287 | __be16 port, u32 info, u8 *payload) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | { |
| 289 | struct inet_sock *inet = inet_sk(sk); |
| 290 | struct sock_exterr_skb *serr; |
| 291 | |
| 292 | if (!inet->recverr) |
| 293 | return; |
| 294 | |
| 295 | skb = skb_clone(skb, GFP_ATOMIC); |
| 296 | if (!skb) |
| 297 | return; |
| 298 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 299 | serr = SKB_EXT_ERR(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | serr->ee.ee_errno = err; |
| 301 | serr->ee.ee_origin = SO_EE_ORIGIN_ICMP; |
Arnaldo Carvalho de Melo | 88c7664 | 2007-03-13 14:43:18 -0300 | [diff] [blame] | 302 | serr->ee.ee_type = icmp_hdr(skb)->type; |
| 303 | serr->ee.ee_code = icmp_hdr(skb)->code; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | serr->ee.ee_pad = 0; |
| 305 | serr->ee.ee_info = info; |
| 306 | serr->ee.ee_data = 0; |
Arnaldo Carvalho de Melo | 88c7664 | 2007-03-13 14:43:18 -0300 | [diff] [blame] | 307 | serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) - |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 308 | skb_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | serr->port = port; |
| 310 | |
Arnaldo Carvalho de Melo | bd82393 | 2007-03-13 17:10:43 -0300 | [diff] [blame] | 311 | if (skb_pull(skb, payload - skb->data) != NULL) { |
| 312 | skb_reset_transport_header(skb); |
| 313 | if (sock_queue_err_skb(sk, skb) == 0) |
| 314 | return; |
| 315 | } |
| 316 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Al Viro | 0579016 | 2006-09-27 18:33:40 -0700 | [diff] [blame] | 319 | void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | { |
| 321 | struct inet_sock *inet = inet_sk(sk); |
| 322 | struct sock_exterr_skb *serr; |
| 323 | struct iphdr *iph; |
| 324 | struct sk_buff *skb; |
| 325 | |
| 326 | if (!inet->recverr) |
| 327 | return; |
| 328 | |
| 329 | skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC); |
| 330 | if (!skb) |
| 331 | return; |
| 332 | |
Arnaldo Carvalho de Melo | 2ca9e6f | 2007-03-10 19:15:25 -0300 | [diff] [blame] | 333 | skb_put(skb, sizeof(struct iphdr)); |
| 334 | skb_reset_network_header(skb); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 335 | iph = ip_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | iph->daddr = daddr; |
| 337 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 338 | serr = SKB_EXT_ERR(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | serr->ee.ee_errno = err; |
| 340 | serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 341 | serr->ee.ee_type = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | serr->ee.ee_code = 0; |
| 343 | serr->ee.ee_pad = 0; |
| 344 | serr->ee.ee_info = info; |
| 345 | serr->ee.ee_data = 0; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 346 | serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | serr->port = port; |
| 348 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 349 | __skb_pull(skb, skb_tail_pointer(skb) - skb->data); |
Arnaldo Carvalho de Melo | bd82393 | 2007-03-13 17:10:43 -0300 | [diff] [blame] | 350 | skb_reset_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | |
| 352 | if (sock_queue_err_skb(sk, skb)) |
| 353 | kfree_skb(skb); |
| 354 | } |
| 355 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 356 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | * Handle MSG_ERRQUEUE |
| 358 | */ |
| 359 | int ip_recv_error(struct sock *sk, struct msghdr *msg, int len) |
| 360 | { |
| 361 | struct sock_exterr_skb *serr; |
| 362 | struct sk_buff *skb, *skb2; |
| 363 | struct sockaddr_in *sin; |
| 364 | struct { |
| 365 | struct sock_extended_err ee; |
| 366 | struct sockaddr_in offender; |
| 367 | } errhdr; |
| 368 | int err; |
| 369 | int copied; |
| 370 | |
| 371 | err = -EAGAIN; |
| 372 | skb = skb_dequeue(&sk->sk_error_queue); |
| 373 | if (skb == NULL) |
| 374 | goto out; |
| 375 | |
| 376 | copied = skb->len; |
| 377 | if (copied > len) { |
| 378 | msg->msg_flags |= MSG_TRUNC; |
| 379 | copied = len; |
| 380 | } |
| 381 | err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); |
| 382 | if (err) |
| 383 | goto out_free_skb; |
| 384 | |
| 385 | sock_recv_timestamp(msg, sk, skb); |
| 386 | |
| 387 | serr = SKB_EXT_ERR(skb); |
| 388 | |
| 389 | sin = (struct sockaddr_in *)msg->msg_name; |
| 390 | if (sin) { |
| 391 | sin->sin_family = AF_INET; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 392 | sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) + |
| 393 | serr->addr_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | sin->sin_port = serr->port; |
| 395 | memset(&sin->sin_zero, 0, sizeof(sin->sin_zero)); |
| 396 | } |
| 397 | |
| 398 | memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err)); |
| 399 | sin = &errhdr.offender; |
| 400 | sin->sin_family = AF_UNSPEC; |
| 401 | if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP) { |
| 402 | struct inet_sock *inet = inet_sk(sk); |
| 403 | |
| 404 | sin->sin_family = AF_INET; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 405 | sin->sin_addr.s_addr = ip_hdr(skb)->saddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | sin->sin_port = 0; |
| 407 | memset(&sin->sin_zero, 0, sizeof(sin->sin_zero)); |
| 408 | if (inet->cmsg_flags) |
| 409 | ip_cmsg_recv(msg, skb); |
| 410 | } |
| 411 | |
| 412 | put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr); |
| 413 | |
| 414 | /* Now we could try to dump offended packet options */ |
| 415 | |
| 416 | msg->msg_flags |= MSG_ERRQUEUE; |
| 417 | err = copied; |
| 418 | |
| 419 | /* Reset and regenerate socket error */ |
Herbert Xu | e0f9f85 | 2005-06-18 22:56:18 -0700 | [diff] [blame] | 420 | spin_lock_bh(&sk->sk_error_queue.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | sk->sk_err = 0; |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 422 | skb2 = skb_peek(&sk->sk_error_queue); |
| 423 | if (skb2 != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno; |
Herbert Xu | e0f9f85 | 2005-06-18 22:56:18 -0700 | [diff] [blame] | 425 | spin_unlock_bh(&sk->sk_error_queue.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | sk->sk_error_report(sk); |
| 427 | } else |
Herbert Xu | e0f9f85 | 2005-06-18 22:56:18 -0700 | [diff] [blame] | 428 | spin_unlock_bh(&sk->sk_error_queue.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 430 | out_free_skb: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | kfree_skb(skb); |
| 432 | out: |
| 433 | return err; |
| 434 | } |
| 435 | |
| 436 | |
| 437 | /* |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 438 | * Socket option code for IP. This is the end of the line after any |
| 439 | * TCP,UDP etc options on an IP socket. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | */ |
| 441 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 442 | static int do_ip_setsockopt(struct sock *sk, int level, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 443 | int optname, char __user *optval, unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | { |
| 445 | struct inet_sock *inet = inet_sk(sk); |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 446 | int val = 0, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 448 | if (((1<<optname) & ((1<<IP_PKTINFO) | (1<<IP_RECVTTL) | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 449 | (1<<IP_RECVOPTS) | (1<<IP_RECVTOS) | |
| 450 | (1<<IP_RETOPTS) | (1<<IP_TOS) | |
| 451 | (1<<IP_TTL) | (1<<IP_HDRINCL) | |
| 452 | (1<<IP_MTU_DISCOVER) | (1<<IP_RECVERR) | |
| 453 | (1<<IP_ROUTER_ALERT) | (1<<IP_FREEBIND) | |
KOVACS Krisztian | f5715ae | 2008-10-01 07:30:02 -0700 | [diff] [blame] | 454 | (1<<IP_PASSSEC) | (1<<IP_TRANSPARENT))) || |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 455 | optname == IP_MULTICAST_TTL || |
Nivedita Singhvi | f771bef | 2009-05-28 07:00:46 +0000 | [diff] [blame] | 456 | optname == IP_MULTICAST_ALL || |
Balazs Scheidler | e8b2dfe | 2008-11-16 19:32:39 -0800 | [diff] [blame] | 457 | optname == IP_MULTICAST_LOOP || |
| 458 | optname == IP_RECVORIGDSTADDR) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | if (optlen >= sizeof(int)) { |
| 460 | if (get_user(val, (int __user *) optval)) |
| 461 | return -EFAULT; |
| 462 | } else if (optlen >= sizeof(char)) { |
| 463 | unsigned char ucval; |
| 464 | |
| 465 | if (get_user(ucval, (unsigned char __user *) optval)) |
| 466 | return -EFAULT; |
| 467 | val = (int) ucval; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | /* If optlen==0, it is equivalent to val == 0 */ |
| 472 | |
Pavel Emelyanov | 6a9fb94 | 2007-11-05 21:32:31 -0800 | [diff] [blame] | 473 | if (ip_mroute_opt(optname)) |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 474 | return ip_mroute_setsockopt(sk, optname, optval, optlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | |
| 476 | err = 0; |
| 477 | lock_sock(sk); |
| 478 | |
| 479 | switch (optname) { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 480 | case IP_OPTIONS: |
| 481 | { |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 482 | struct ip_options *opt = NULL; |
roel kluin | 65a1c4f | 2009-10-23 05:59:21 +0000 | [diff] [blame] | 483 | if (optlen > 40) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 484 | goto e_inval; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 485 | err = ip_options_get_from_user(sock_net(sk), &opt, |
Denis V. Lunev | cb84663 | 2008-03-24 15:31:00 -0700 | [diff] [blame] | 486 | optval, optlen); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 487 | if (err) |
| 488 | break; |
| 489 | if (inet->is_icsk) { |
| 490 | struct inet_connection_sock *icsk = inet_csk(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 492 | if (sk->sk_family == PF_INET || |
| 493 | (!((1 << sk->sk_state) & |
| 494 | (TCPF_LISTEN | TCPF_CLOSE)) && |
Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 495 | inet->inet_daddr != LOOPBACK4_IPV6)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | #endif |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 497 | if (inet->opt) |
| 498 | icsk->icsk_ext_hdr_len -= inet->opt->optlen; |
| 499 | if (opt) |
| 500 | icsk->icsk_ext_hdr_len += opt->optlen; |
| 501 | icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 503 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 506 | opt = xchg(&inet->opt, opt); |
| 507 | kfree(opt); |
| 508 | break; |
| 509 | } |
| 510 | case IP_PKTINFO: |
| 511 | if (val) |
| 512 | inet->cmsg_flags |= IP_CMSG_PKTINFO; |
| 513 | else |
| 514 | inet->cmsg_flags &= ~IP_CMSG_PKTINFO; |
| 515 | break; |
| 516 | case IP_RECVTTL: |
| 517 | if (val) |
| 518 | inet->cmsg_flags |= IP_CMSG_TTL; |
| 519 | else |
| 520 | inet->cmsg_flags &= ~IP_CMSG_TTL; |
| 521 | break; |
| 522 | case IP_RECVTOS: |
| 523 | if (val) |
| 524 | inet->cmsg_flags |= IP_CMSG_TOS; |
| 525 | else |
| 526 | inet->cmsg_flags &= ~IP_CMSG_TOS; |
| 527 | break; |
| 528 | case IP_RECVOPTS: |
| 529 | if (val) |
| 530 | inet->cmsg_flags |= IP_CMSG_RECVOPTS; |
| 531 | else |
| 532 | inet->cmsg_flags &= ~IP_CMSG_RECVOPTS; |
| 533 | break; |
| 534 | case IP_RETOPTS: |
| 535 | if (val) |
| 536 | inet->cmsg_flags |= IP_CMSG_RETOPTS; |
| 537 | else |
| 538 | inet->cmsg_flags &= ~IP_CMSG_RETOPTS; |
| 539 | break; |
| 540 | case IP_PASSSEC: |
| 541 | if (val) |
| 542 | inet->cmsg_flags |= IP_CMSG_PASSSEC; |
| 543 | else |
| 544 | inet->cmsg_flags &= ~IP_CMSG_PASSSEC; |
| 545 | break; |
Balazs Scheidler | e8b2dfe | 2008-11-16 19:32:39 -0800 | [diff] [blame] | 546 | case IP_RECVORIGDSTADDR: |
| 547 | if (val) |
| 548 | inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR; |
| 549 | else |
| 550 | inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR; |
| 551 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 552 | case IP_TOS: /* This sets both TOS and Precedence */ |
| 553 | if (sk->sk_type == SOCK_STREAM) { |
| 554 | val &= ~3; |
| 555 | val |= inet->tos & 3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 557 | if (inet->tos != val) { |
| 558 | inet->tos = val; |
| 559 | sk->sk_priority = rt_tos2priority(val); |
| 560 | sk_dst_reset(sk); |
| 561 | } |
| 562 | break; |
| 563 | case IP_TTL: |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 564 | if (optlen < 1) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 565 | goto e_inval; |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 566 | if (val != -1 && (val < 0 || val > 255)) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 567 | goto e_inval; |
| 568 | inet->uc_ttl = val; |
| 569 | break; |
| 570 | case IP_HDRINCL: |
| 571 | if (sk->sk_type != SOCK_RAW) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | err = -ENOPROTOOPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 574 | } |
| 575 | inet->hdrincl = val ? 1 : 0; |
| 576 | break; |
| 577 | case IP_MTU_DISCOVER: |
John Dykstra | 0eae750 | 2009-10-19 21:53:53 -0700 | [diff] [blame] | 578 | if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_PROBE) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 579 | goto e_inval; |
| 580 | inet->pmtudisc = val; |
| 581 | break; |
| 582 | case IP_RECVERR: |
| 583 | inet->recverr = !!val; |
| 584 | if (!val) |
| 585 | skb_queue_purge(&sk->sk_error_queue); |
| 586 | break; |
| 587 | case IP_MULTICAST_TTL: |
| 588 | if (sk->sk_type == SOCK_STREAM) |
| 589 | goto e_inval; |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 590 | if (optlen < 1) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 591 | goto e_inval; |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 592 | if (val == -1) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 593 | val = 1; |
| 594 | if (val < 0 || val > 255) |
| 595 | goto e_inval; |
| 596 | inet->mc_ttl = val; |
| 597 | break; |
| 598 | case IP_MULTICAST_LOOP: |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 599 | if (optlen < 1) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 600 | goto e_inval; |
| 601 | inet->mc_loop = !!val; |
| 602 | break; |
| 603 | case IP_MULTICAST_IF: |
| 604 | { |
| 605 | struct ip_mreqn mreq; |
| 606 | struct net_device *dev = NULL; |
| 607 | |
| 608 | if (sk->sk_type == SOCK_STREAM) |
| 609 | goto e_inval; |
| 610 | /* |
| 611 | * Check the arguments are allowable |
| 612 | */ |
| 613 | |
Shan Wei | 0915921 | 2009-09-22 15:41:10 +0000 | [diff] [blame] | 614 | if (optlen < sizeof(struct in_addr)) |
| 615 | goto e_inval; |
| 616 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 617 | err = -EFAULT; |
| 618 | if (optlen >= sizeof(struct ip_mreqn)) { |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 619 | if (copy_from_user(&mreq, optval, sizeof(mreq))) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 620 | break; |
| 621 | } else { |
| 622 | memset(&mreq, 0, sizeof(mreq)); |
| 623 | if (optlen >= sizeof(struct in_addr) && |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 624 | copy_from_user(&mreq.imr_address, optval, |
| 625 | sizeof(struct in_addr))) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 626 | break; |
| 627 | } |
| 628 | |
| 629 | if (!mreq.imr_ifindex) { |
Al Viro | e6f1ceb | 2008-03-17 22:44:53 -0700 | [diff] [blame] | 630 | if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 631 | inet->mc_index = 0; |
| 632 | inet->mc_addr = 0; |
| 633 | err = 0; |
| 634 | break; |
| 635 | } |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 636 | dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr); |
Eric Dumazet | 55b8050 | 2009-10-19 06:41:58 +0000 | [diff] [blame] | 637 | if (dev) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 638 | mreq.imr_ifindex = dev->ifindex; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 639 | } else |
Eric Dumazet | 55b8050 | 2009-10-19 06:41:58 +0000 | [diff] [blame] | 640 | dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 641 | |
| 642 | |
| 643 | err = -EADDRNOTAVAIL; |
| 644 | if (!dev) |
| 645 | break; |
Eric Dumazet | 55b8050 | 2009-10-19 06:41:58 +0000 | [diff] [blame] | 646 | dev_put(dev); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 647 | |
| 648 | err = -EINVAL; |
| 649 | if (sk->sk_bound_dev_if && |
| 650 | mreq.imr_ifindex != sk->sk_bound_dev_if) |
| 651 | break; |
| 652 | |
| 653 | inet->mc_index = mreq.imr_ifindex; |
| 654 | inet->mc_addr = mreq.imr_address.s_addr; |
| 655 | err = 0; |
| 656 | break; |
| 657 | } |
| 658 | |
| 659 | case IP_ADD_MEMBERSHIP: |
| 660 | case IP_DROP_MEMBERSHIP: |
| 661 | { |
| 662 | struct ip_mreqn mreq; |
| 663 | |
Flavio Leitner | a96fb49 | 2007-08-24 22:16:39 -0700 | [diff] [blame] | 664 | err = -EPROTO; |
| 665 | if (inet_sk(sk)->is_icsk) |
| 666 | break; |
| 667 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 668 | if (optlen < sizeof(struct ip_mreq)) |
| 669 | goto e_inval; |
| 670 | err = -EFAULT; |
| 671 | if (optlen >= sizeof(struct ip_mreqn)) { |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 672 | if (copy_from_user(&mreq, optval, sizeof(mreq))) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 673 | break; |
| 674 | } else { |
| 675 | memset(&mreq, 0, sizeof(mreq)); |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 676 | if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq))) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 677 | break; |
| 678 | } |
| 679 | |
| 680 | if (optname == IP_ADD_MEMBERSHIP) |
| 681 | err = ip_mc_join_group(sk, &mreq); |
| 682 | else |
| 683 | err = ip_mc_leave_group(sk, &mreq); |
| 684 | break; |
| 685 | } |
| 686 | case IP_MSFILTER: |
| 687 | { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 688 | struct ip_msfilter *msf; |
| 689 | |
| 690 | if (optlen < IP_MSFILTER_SIZE(0)) |
| 691 | goto e_inval; |
| 692 | if (optlen > sysctl_optmem_max) { |
| 693 | err = -ENOBUFS; |
| 694 | break; |
| 695 | } |
| 696 | msf = kmalloc(optlen, GFP_KERNEL); |
Stephen Hemminger | cfcabdc | 2007-10-09 01:59:42 -0700 | [diff] [blame] | 697 | if (!msf) { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 698 | err = -ENOBUFS; |
| 699 | break; |
| 700 | } |
| 701 | err = -EFAULT; |
| 702 | if (copy_from_user(msf, optval, optlen)) { |
| 703 | kfree(msf); |
| 704 | break; |
| 705 | } |
| 706 | /* numsrc >= (1G-4) overflow in 32 bits */ |
| 707 | if (msf->imsf_numsrc >= 0x3ffffffcU || |
| 708 | msf->imsf_numsrc > sysctl_igmp_max_msf) { |
| 709 | kfree(msf); |
| 710 | err = -ENOBUFS; |
| 711 | break; |
| 712 | } |
| 713 | if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) { |
| 714 | kfree(msf); |
| 715 | err = -EINVAL; |
| 716 | break; |
| 717 | } |
| 718 | err = ip_mc_msfilter(sk, msf, 0); |
| 719 | kfree(msf); |
| 720 | break; |
| 721 | } |
| 722 | case IP_BLOCK_SOURCE: |
| 723 | case IP_UNBLOCK_SOURCE: |
| 724 | case IP_ADD_SOURCE_MEMBERSHIP: |
| 725 | case IP_DROP_SOURCE_MEMBERSHIP: |
| 726 | { |
| 727 | struct ip_mreq_source mreqs; |
| 728 | int omode, add; |
| 729 | |
| 730 | if (optlen != sizeof(struct ip_mreq_source)) |
| 731 | goto e_inval; |
| 732 | if (copy_from_user(&mreqs, optval, sizeof(mreqs))) { |
| 733 | err = -EFAULT; |
| 734 | break; |
| 735 | } |
| 736 | if (optname == IP_BLOCK_SOURCE) { |
| 737 | omode = MCAST_EXCLUDE; |
| 738 | add = 1; |
| 739 | } else if (optname == IP_UNBLOCK_SOURCE) { |
| 740 | omode = MCAST_EXCLUDE; |
| 741 | add = 0; |
| 742 | } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) { |
| 743 | struct ip_mreqn mreq; |
| 744 | |
| 745 | mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr; |
| 746 | mreq.imr_address.s_addr = mreqs.imr_interface; |
| 747 | mreq.imr_ifindex = 0; |
| 748 | err = ip_mc_join_group(sk, &mreq); |
| 749 | if (err && err != -EADDRINUSE) |
| 750 | break; |
| 751 | omode = MCAST_INCLUDE; |
| 752 | add = 1; |
| 753 | } else /* IP_DROP_SOURCE_MEMBERSHIP */ { |
| 754 | omode = MCAST_INCLUDE; |
| 755 | add = 0; |
| 756 | } |
| 757 | err = ip_mc_source(add, omode, sk, &mreqs, 0); |
| 758 | break; |
| 759 | } |
| 760 | case MCAST_JOIN_GROUP: |
| 761 | case MCAST_LEAVE_GROUP: |
| 762 | { |
| 763 | struct group_req greq; |
| 764 | struct sockaddr_in *psin; |
| 765 | struct ip_mreqn mreq; |
| 766 | |
| 767 | if (optlen < sizeof(struct group_req)) |
| 768 | goto e_inval; |
| 769 | err = -EFAULT; |
| 770 | if (copy_from_user(&greq, optval, sizeof(greq))) |
| 771 | break; |
| 772 | psin = (struct sockaddr_in *)&greq.gr_group; |
| 773 | if (psin->sin_family != AF_INET) |
| 774 | goto e_inval; |
| 775 | memset(&mreq, 0, sizeof(mreq)); |
| 776 | mreq.imr_multiaddr = psin->sin_addr; |
| 777 | mreq.imr_ifindex = greq.gr_interface; |
| 778 | |
| 779 | if (optname == MCAST_JOIN_GROUP) |
| 780 | err = ip_mc_join_group(sk, &mreq); |
| 781 | else |
| 782 | err = ip_mc_leave_group(sk, &mreq); |
| 783 | break; |
| 784 | } |
| 785 | case MCAST_JOIN_SOURCE_GROUP: |
| 786 | case MCAST_LEAVE_SOURCE_GROUP: |
| 787 | case MCAST_BLOCK_SOURCE: |
| 788 | case MCAST_UNBLOCK_SOURCE: |
| 789 | { |
| 790 | struct group_source_req greqs; |
| 791 | struct ip_mreq_source mreqs; |
| 792 | struct sockaddr_in *psin; |
| 793 | int omode, add; |
| 794 | |
| 795 | if (optlen != sizeof(struct group_source_req)) |
| 796 | goto e_inval; |
| 797 | if (copy_from_user(&greqs, optval, sizeof(greqs))) { |
| 798 | err = -EFAULT; |
| 799 | break; |
| 800 | } |
| 801 | if (greqs.gsr_group.ss_family != AF_INET || |
| 802 | greqs.gsr_source.ss_family != AF_INET) { |
| 803 | err = -EADDRNOTAVAIL; |
| 804 | break; |
| 805 | } |
| 806 | psin = (struct sockaddr_in *)&greqs.gsr_group; |
| 807 | mreqs.imr_multiaddr = psin->sin_addr.s_addr; |
| 808 | psin = (struct sockaddr_in *)&greqs.gsr_source; |
| 809 | mreqs.imr_sourceaddr = psin->sin_addr.s_addr; |
| 810 | mreqs.imr_interface = 0; /* use index for mc_source */ |
| 811 | |
| 812 | if (optname == MCAST_BLOCK_SOURCE) { |
| 813 | omode = MCAST_EXCLUDE; |
| 814 | add = 1; |
| 815 | } else if (optname == MCAST_UNBLOCK_SOURCE) { |
| 816 | omode = MCAST_EXCLUDE; |
| 817 | add = 0; |
| 818 | } else if (optname == MCAST_JOIN_SOURCE_GROUP) { |
| 819 | struct ip_mreqn mreq; |
| 820 | |
| 821 | psin = (struct sockaddr_in *)&greqs.gsr_group; |
| 822 | mreq.imr_multiaddr = psin->sin_addr; |
| 823 | mreq.imr_address.s_addr = 0; |
| 824 | mreq.imr_ifindex = greqs.gsr_interface; |
| 825 | err = ip_mc_join_group(sk, &mreq); |
| 826 | if (err && err != -EADDRINUSE) |
| 827 | break; |
| 828 | greqs.gsr_interface = mreq.imr_ifindex; |
| 829 | omode = MCAST_INCLUDE; |
| 830 | add = 1; |
| 831 | } else /* MCAST_LEAVE_SOURCE_GROUP */ { |
| 832 | omode = MCAST_INCLUDE; |
| 833 | add = 0; |
| 834 | } |
| 835 | err = ip_mc_source(add, omode, sk, &mreqs, |
| 836 | greqs.gsr_interface); |
| 837 | break; |
| 838 | } |
| 839 | case MCAST_MSFILTER: |
| 840 | { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 841 | struct sockaddr_in *psin; |
| 842 | struct ip_msfilter *msf = NULL; |
| 843 | struct group_filter *gsf = NULL; |
| 844 | int msize, i, ifindex; |
| 845 | |
| 846 | if (optlen < GROUP_FILTER_SIZE(0)) |
| 847 | goto e_inval; |
| 848 | if (optlen > sysctl_optmem_max) { |
| 849 | err = -ENOBUFS; |
| 850 | break; |
| 851 | } |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 852 | gsf = kmalloc(optlen, GFP_KERNEL); |
Stephen Hemminger | cfcabdc | 2007-10-09 01:59:42 -0700 | [diff] [blame] | 853 | if (!gsf) { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 854 | err = -ENOBUFS; |
| 855 | break; |
| 856 | } |
| 857 | err = -EFAULT; |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 858 | if (copy_from_user(gsf, optval, optlen)) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 859 | goto mc_msf_out; |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 860 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 861 | /* numsrc >= (4G-140)/128 overflow in 32 bits */ |
| 862 | if (gsf->gf_numsrc >= 0x1ffffff || |
| 863 | gsf->gf_numsrc > sysctl_igmp_max_msf) { |
| 864 | err = -ENOBUFS; |
| 865 | goto mc_msf_out; |
| 866 | } |
| 867 | if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) { |
| 868 | err = -EINVAL; |
| 869 | goto mc_msf_out; |
| 870 | } |
| 871 | msize = IP_MSFILTER_SIZE(gsf->gf_numsrc); |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 872 | msf = kmalloc(msize, GFP_KERNEL); |
Stephen Hemminger | cfcabdc | 2007-10-09 01:59:42 -0700 | [diff] [blame] | 873 | if (!msf) { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 874 | err = -ENOBUFS; |
| 875 | goto mc_msf_out; |
| 876 | } |
| 877 | ifindex = gsf->gf_interface; |
| 878 | psin = (struct sockaddr_in *)&gsf->gf_group; |
| 879 | if (psin->sin_family != AF_INET) { |
| 880 | err = -EADDRNOTAVAIL; |
| 881 | goto mc_msf_out; |
| 882 | } |
| 883 | msf->imsf_multiaddr = psin->sin_addr.s_addr; |
| 884 | msf->imsf_interface = 0; |
| 885 | msf->imsf_fmode = gsf->gf_fmode; |
| 886 | msf->imsf_numsrc = gsf->gf_numsrc; |
| 887 | err = -EADDRNOTAVAIL; |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 888 | for (i = 0; i < gsf->gf_numsrc; ++i) { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 889 | psin = (struct sockaddr_in *)&gsf->gf_slist[i]; |
| 890 | |
| 891 | if (psin->sin_family != AF_INET) |
| 892 | goto mc_msf_out; |
| 893 | msf->imsf_slist[i] = psin->sin_addr.s_addr; |
| 894 | } |
| 895 | kfree(gsf); |
| 896 | gsf = NULL; |
| 897 | |
| 898 | err = ip_mc_msfilter(sk, msf, ifindex); |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 899 | mc_msf_out: |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 900 | kfree(msf); |
| 901 | kfree(gsf); |
| 902 | break; |
| 903 | } |
Nivedita Singhvi | f771bef | 2009-05-28 07:00:46 +0000 | [diff] [blame] | 904 | case IP_MULTICAST_ALL: |
| 905 | if (optlen < 1) |
| 906 | goto e_inval; |
| 907 | if (val != 0 && val != 1) |
| 908 | goto e_inval; |
| 909 | inet->mc_all = val; |
| 910 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 911 | case IP_ROUTER_ALERT: |
| 912 | err = ip_ra_control(sk, val ? 1 : 0, NULL); |
| 913 | break; |
| 914 | |
| 915 | case IP_FREEBIND: |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 916 | if (optlen < 1) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 917 | goto e_inval; |
| 918 | inet->freebind = !!val; |
| 919 | break; |
| 920 | |
| 921 | case IP_IPSEC_POLICY: |
| 922 | case IP_XFRM_POLICY: |
| 923 | err = -EPERM; |
| 924 | if (!capable(CAP_NET_ADMIN)) |
| 925 | break; |
| 926 | err = xfrm_user_policy(sk, optname, optval, optlen); |
| 927 | break; |
| 928 | |
KOVACS Krisztian | f5715ae | 2008-10-01 07:30:02 -0700 | [diff] [blame] | 929 | case IP_TRANSPARENT: |
| 930 | if (!capable(CAP_NET_ADMIN)) { |
| 931 | err = -EPERM; |
| 932 | break; |
| 933 | } |
| 934 | if (optlen < 1) |
| 935 | goto e_inval; |
| 936 | inet->transparent = !!val; |
| 937 | break; |
| 938 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 939 | default: |
| 940 | err = -ENOPROTOOPT; |
| 941 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | } |
| 943 | release_sock(sk); |
| 944 | return err; |
| 945 | |
| 946 | e_inval: |
| 947 | release_sock(sk); |
| 948 | return -EINVAL; |
| 949 | } |
| 950 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 951 | int ip_setsockopt(struct sock *sk, int level, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 952 | int optname, char __user *optval, unsigned int optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 953 | { |
| 954 | int err; |
| 955 | |
| 956 | if (level != SOL_IP) |
| 957 | return -ENOPROTOOPT; |
| 958 | |
| 959 | err = do_ip_setsockopt(sk, level, optname, optval, optlen); |
| 960 | #ifdef CONFIG_NETFILTER |
| 961 | /* we need to exclude all possible ENOPROTOOPTs except default case */ |
| 962 | if (err == -ENOPROTOOPT && optname != IP_HDRINCL && |
Pavel Emelyanov | 6a9fb94 | 2007-11-05 21:32:31 -0800 | [diff] [blame] | 963 | optname != IP_IPSEC_POLICY && |
| 964 | optname != IP_XFRM_POLICY && |
| 965 | !ip_mroute_opt(optname)) { |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 966 | lock_sock(sk); |
| 967 | err = nf_setsockopt(sk, PF_INET, optname, optval, optlen); |
| 968 | release_sock(sk); |
| 969 | } |
| 970 | #endif |
| 971 | return err; |
| 972 | } |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 973 | EXPORT_SYMBOL(ip_setsockopt); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 974 | |
| 975 | #ifdef CONFIG_COMPAT |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 976 | int compat_ip_setsockopt(struct sock *sk, int level, int optname, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 977 | char __user *optval, unsigned int optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 978 | { |
| 979 | int err; |
| 980 | |
| 981 | if (level != SOL_IP) |
| 982 | return -ENOPROTOOPT; |
| 983 | |
David L Stevens | dae5029 | 2008-04-27 01:06:07 -0700 | [diff] [blame] | 984 | if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER) |
| 985 | return compat_mc_setsockopt(sk, level, optname, optval, optlen, |
| 986 | ip_setsockopt); |
| 987 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 988 | err = do_ip_setsockopt(sk, level, optname, optval, optlen); |
| 989 | #ifdef CONFIG_NETFILTER |
| 990 | /* we need to exclude all possible ENOPROTOOPTs except default case */ |
| 991 | if (err == -ENOPROTOOPT && optname != IP_HDRINCL && |
Pavel Emelyanov | 6a9fb94 | 2007-11-05 21:32:31 -0800 | [diff] [blame] | 992 | optname != IP_IPSEC_POLICY && |
| 993 | optname != IP_XFRM_POLICY && |
| 994 | !ip_mroute_opt(optname)) { |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 995 | lock_sock(sk); |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 996 | err = compat_nf_setsockopt(sk, PF_INET, optname, |
| 997 | optval, optlen); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 998 | release_sock(sk); |
| 999 | } |
| 1000 | #endif |
| 1001 | return err; |
| 1002 | } |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1003 | EXPORT_SYMBOL(compat_ip_setsockopt); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1004 | #endif |
| 1005 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | /* |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 1007 | * Get the options. Note for future reference. The GET of IP options gets |
| 1008 | * the _received_ ones. The set sets the _sent_ ones. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | */ |
| 1010 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1011 | static int do_ip_getsockopt(struct sock *sk, int level, int optname, |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1012 | char __user *optval, int __user *optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | { |
| 1014 | struct inet_sock *inet = inet_sk(sk); |
| 1015 | int val; |
| 1016 | int len; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1017 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1018 | if (level != SOL_IP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | return -EOPNOTSUPP; |
| 1020 | |
Pavel Emelyanov | 6a9fb94 | 2007-11-05 21:32:31 -0800 | [diff] [blame] | 1021 | if (ip_mroute_opt(optname)) |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 1022 | return ip_mroute_getsockopt(sk, optname, optval, optlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 1024 | if (get_user(len, optlen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | return -EFAULT; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1026 | if (len < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | return -EINVAL; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1028 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | lock_sock(sk); |
| 1030 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1031 | switch (optname) { |
| 1032 | case IP_OPTIONS: |
| 1033 | { |
| 1034 | unsigned char optbuf[sizeof(struct ip_options)+40]; |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 1035 | struct ip_options * opt = (struct ip_options *)optbuf; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1036 | opt->optlen = 0; |
| 1037 | if (inet->opt) |
| 1038 | memcpy(optbuf, inet->opt, |
| 1039 | sizeof(struct ip_options)+ |
| 1040 | inet->opt->optlen); |
| 1041 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1043 | if (opt->optlen == 0) |
| 1044 | return put_user(0, optlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1046 | ip_options_undo(opt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1048 | len = min_t(unsigned int, len, opt->optlen); |
| 1049 | if (put_user(len, optlen)) |
| 1050 | return -EFAULT; |
| 1051 | if (copy_to_user(optval, opt->__data, len)) |
| 1052 | return -EFAULT; |
| 1053 | return 0; |
| 1054 | } |
| 1055 | case IP_PKTINFO: |
| 1056 | val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0; |
| 1057 | break; |
| 1058 | case IP_RECVTTL: |
| 1059 | val = (inet->cmsg_flags & IP_CMSG_TTL) != 0; |
| 1060 | break; |
| 1061 | case IP_RECVTOS: |
| 1062 | val = (inet->cmsg_flags & IP_CMSG_TOS) != 0; |
| 1063 | break; |
| 1064 | case IP_RECVOPTS: |
| 1065 | val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0; |
| 1066 | break; |
| 1067 | case IP_RETOPTS: |
| 1068 | val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0; |
| 1069 | break; |
| 1070 | case IP_PASSSEC: |
| 1071 | val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0; |
| 1072 | break; |
Balazs Scheidler | e8b2dfe | 2008-11-16 19:32:39 -0800 | [diff] [blame] | 1073 | case IP_RECVORIGDSTADDR: |
| 1074 | val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0; |
| 1075 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1076 | case IP_TOS: |
| 1077 | val = inet->tos; |
| 1078 | break; |
| 1079 | case IP_TTL: |
| 1080 | val = (inet->uc_ttl == -1 ? |
| 1081 | sysctl_ip_default_ttl : |
| 1082 | inet->uc_ttl); |
| 1083 | break; |
| 1084 | case IP_HDRINCL: |
| 1085 | val = inet->hdrincl; |
| 1086 | break; |
| 1087 | case IP_MTU_DISCOVER: |
| 1088 | val = inet->pmtudisc; |
| 1089 | break; |
| 1090 | case IP_MTU: |
| 1091 | { |
| 1092 | struct dst_entry *dst; |
| 1093 | val = 0; |
| 1094 | dst = sk_dst_get(sk); |
| 1095 | if (dst) { |
| 1096 | val = dst_mtu(dst); |
| 1097 | dst_release(dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1099 | if (!val) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | release_sock(sk); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1101 | return -ENOTCONN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1103 | break; |
| 1104 | } |
| 1105 | case IP_RECVERR: |
| 1106 | val = inet->recverr; |
| 1107 | break; |
| 1108 | case IP_MULTICAST_TTL: |
| 1109 | val = inet->mc_ttl; |
| 1110 | break; |
| 1111 | case IP_MULTICAST_LOOP: |
| 1112 | val = inet->mc_loop; |
| 1113 | break; |
| 1114 | case IP_MULTICAST_IF: |
| 1115 | { |
| 1116 | struct in_addr addr; |
| 1117 | len = min_t(unsigned int, len, sizeof(struct in_addr)); |
| 1118 | addr.s_addr = inet->mc_addr; |
| 1119 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1121 | if (put_user(len, optlen)) |
| 1122 | return -EFAULT; |
| 1123 | if (copy_to_user(optval, &addr, len)) |
| 1124 | return -EFAULT; |
| 1125 | return 0; |
| 1126 | } |
| 1127 | case IP_MSFILTER: |
| 1128 | { |
| 1129 | struct ip_msfilter msf; |
| 1130 | int err; |
| 1131 | |
| 1132 | if (len < IP_MSFILTER_SIZE(0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | release_sock(sk); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1134 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1136 | if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | release_sock(sk); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1138 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1140 | err = ip_mc_msfget(sk, &msf, |
| 1141 | (struct ip_msfilter __user *)optval, optlen); |
| 1142 | release_sock(sk); |
| 1143 | return err; |
| 1144 | } |
| 1145 | case MCAST_MSFILTER: |
| 1146 | { |
| 1147 | struct group_filter gsf; |
| 1148 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1150 | if (len < GROUP_FILTER_SIZE(0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | release_sock(sk); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1152 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1154 | if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | release_sock(sk); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1156 | return -EFAULT; |
| 1157 | } |
| 1158 | err = ip_mc_gsfget(sk, &gsf, |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 1159 | (struct group_filter __user *)optval, |
| 1160 | optlen); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1161 | release_sock(sk); |
| 1162 | return err; |
| 1163 | } |
Nivedita Singhvi | f771bef | 2009-05-28 07:00:46 +0000 | [diff] [blame] | 1164 | case IP_MULTICAST_ALL: |
| 1165 | val = inet->mc_all; |
| 1166 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1167 | case IP_PKTOPTIONS: |
| 1168 | { |
| 1169 | struct msghdr msg; |
| 1170 | |
| 1171 | release_sock(sk); |
| 1172 | |
| 1173 | if (sk->sk_type != SOCK_STREAM) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | return -ENOPROTOOPT; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1175 | |
| 1176 | msg.msg_control = optval; |
| 1177 | msg.msg_controllen = len; |
| 1178 | msg.msg_flags = 0; |
| 1179 | |
| 1180 | if (inet->cmsg_flags & IP_CMSG_PKTINFO) { |
| 1181 | struct in_pktinfo info; |
| 1182 | |
Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 1183 | info.ipi_addr.s_addr = inet->inet_rcv_saddr; |
| 1184 | info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1185 | info.ipi_ifindex = inet->mc_index; |
| 1186 | put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info); |
| 1187 | } |
| 1188 | if (inet->cmsg_flags & IP_CMSG_TTL) { |
| 1189 | int hlim = inet->mc_ttl; |
| 1190 | put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim); |
| 1191 | } |
| 1192 | len -= msg.msg_controllen; |
| 1193 | return put_user(len, optlen); |
| 1194 | } |
| 1195 | case IP_FREEBIND: |
| 1196 | val = inet->freebind; |
| 1197 | break; |
KOVACS Krisztian | f5715ae | 2008-10-01 07:30:02 -0700 | [diff] [blame] | 1198 | case IP_TRANSPARENT: |
| 1199 | val = inet->transparent; |
| 1200 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1201 | default: |
| 1202 | release_sock(sk); |
| 1203 | return -ENOPROTOOPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | } |
| 1205 | release_sock(sk); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1206 | |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 1207 | if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | unsigned char ucval = (unsigned char)val; |
| 1209 | len = 1; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1210 | if (put_user(len, optlen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | return -EFAULT; |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 1212 | if (copy_to_user(optval, &ucval, 1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | return -EFAULT; |
| 1214 | } else { |
| 1215 | len = min_t(unsigned int, sizeof(int), len); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1216 | if (put_user(len, optlen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | return -EFAULT; |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 1218 | if (copy_to_user(optval, &val, len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | return -EFAULT; |
| 1220 | } |
| 1221 | return 0; |
| 1222 | } |
| 1223 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1224 | int ip_getsockopt(struct sock *sk, int level, |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1225 | int optname, char __user *optval, int __user *optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1226 | { |
| 1227 | int err; |
| 1228 | |
| 1229 | err = do_ip_getsockopt(sk, level, optname, optval, optlen); |
| 1230 | #ifdef CONFIG_NETFILTER |
| 1231 | /* we need to exclude all possible ENOPROTOOPTs except default case */ |
Pavel Emelyanov | 6a9fb94 | 2007-11-05 21:32:31 -0800 | [diff] [blame] | 1232 | if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS && |
| 1233 | !ip_mroute_opt(optname)) { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1234 | int len; |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1235 | |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 1236 | if (get_user(len, optlen)) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1237 | return -EFAULT; |
| 1238 | |
| 1239 | lock_sock(sk); |
| 1240 | err = nf_getsockopt(sk, PF_INET, optname, optval, |
| 1241 | &len); |
| 1242 | release_sock(sk); |
| 1243 | if (err >= 0) |
| 1244 | err = put_user(len, optlen); |
| 1245 | return err; |
| 1246 | } |
| 1247 | #endif |
| 1248 | return err; |
| 1249 | } |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 1250 | EXPORT_SYMBOL(ip_getsockopt); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1251 | |
| 1252 | #ifdef CONFIG_COMPAT |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1253 | int compat_ip_getsockopt(struct sock *sk, int level, int optname, |
| 1254 | char __user *optval, int __user *optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1255 | { |
David L Stevens | 42908c6 | 2008-04-29 03:23:22 -0700 | [diff] [blame] | 1256 | int err; |
| 1257 | |
| 1258 | if (optname == MCAST_MSFILTER) |
| 1259 | return compat_mc_getsockopt(sk, level, optname, optval, optlen, |
| 1260 | ip_getsockopt); |
| 1261 | |
| 1262 | err = do_ip_getsockopt(sk, level, optname, optval, optlen); |
| 1263 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1264 | #ifdef CONFIG_NETFILTER |
| 1265 | /* we need to exclude all possible ENOPROTOOPTs except default case */ |
Pavel Emelyanov | 6a9fb94 | 2007-11-05 21:32:31 -0800 | [diff] [blame] | 1266 | if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS && |
| 1267 | !ip_mroute_opt(optname)) { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1268 | int len; |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1269 | |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1270 | if (get_user(len, optlen)) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1271 | return -EFAULT; |
| 1272 | |
| 1273 | lock_sock(sk); |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1274 | err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1275 | release_sock(sk); |
| 1276 | if (err >= 0) |
| 1277 | err = put_user(len, optlen); |
| 1278 | return err; |
| 1279 | } |
| 1280 | #endif |
| 1281 | return err; |
| 1282 | } |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1283 | EXPORT_SYMBOL(compat_ip_getsockopt); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1284 | #endif |