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