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