Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * INET An implementation of the TCP/IP protocol suite for the LINUX |
| 3 | * operating system. INET is implemented using the BSD Socket |
| 4 | * interface as the means of communication with the user level. |
| 5 | * |
| 6 | * The IP to API glue. |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 7 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * Authors: see ip.c |
| 9 | * |
| 10 | * Fixes: |
| 11 | * Many : Split from ip.c , see ip.c for history. |
| 12 | * Martin Mares : TOS setting fixed. |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 13 | * Alan Cox : Fixed a couple of oopses in Martin's |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * TOS tweaks. |
| 15 | * Mike McLagan : Routing by source |
| 16 | */ |
| 17 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/module.h> |
| 19 | #include <linux/types.h> |
| 20 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/skbuff.h> |
| 22 | #include <linux/ip.h> |
| 23 | #include <linux/icmp.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 24 | #include <linux/inetdevice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/netdevice.h> |
| 26 | #include <net/sock.h> |
| 27 | #include <net/ip.h> |
| 28 | #include <net/icmp.h> |
Arnaldo Carvalho de Melo | d83d846 | 2005-12-13 23:26:10 -0800 | [diff] [blame] | 29 | #include <net/tcp_states.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/udp.h> |
| 31 | #include <linux/igmp.h> |
| 32 | #include <linux/netfilter.h> |
| 33 | #include <linux/route.h> |
| 34 | #include <linux/mroute.h> |
| 35 | #include <net/route.h> |
| 36 | #include <net/xfrm.h> |
David L Stevens | dae5029 | 2008-04-27 01:06:07 -0700 | [diff] [blame] | 37 | #include <net/compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
| 39 | #include <net/transp_v6.h> |
| 40 | #endif |
| 41 | |
| 42 | #include <linux/errqueue.h> |
| 43 | #include <asm/uaccess.h> |
| 44 | |
| 45 | #define IP_CMSG_PKTINFO 1 |
| 46 | #define IP_CMSG_TTL 2 |
| 47 | #define IP_CMSG_TOS 4 |
| 48 | #define IP_CMSG_RECVOPTS 8 |
| 49 | #define IP_CMSG_RETOPTS 16 |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 50 | #define IP_CMSG_PASSSEC 32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | /* |
| 53 | * SOL_IP control messages. |
| 54 | */ |
| 55 | |
| 56 | static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb) |
| 57 | { |
| 58 | struct in_pktinfo info; |
Eric Dumazet | ee6b967 | 2008-03-05 18:30:47 -0800 | [diff] [blame] | 59 | struct rtable *rt = skb->rtable; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 61 | info.ipi_addr.s_addr = ip_hdr(skb)->daddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | if (rt) { |
| 63 | info.ipi_ifindex = rt->rt_iif; |
| 64 | info.ipi_spec_dst.s_addr = rt->rt_spec_dst; |
| 65 | } else { |
| 66 | info.ipi_ifindex = 0; |
| 67 | info.ipi_spec_dst.s_addr = 0; |
| 68 | } |
| 69 | |
| 70 | put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info); |
| 71 | } |
| 72 | |
| 73 | static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb) |
| 74 | { |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 75 | int ttl = ip_hdr(skb)->ttl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl); |
| 77 | } |
| 78 | |
| 79 | static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb) |
| 80 | { |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 81 | put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb) |
| 85 | { |
| 86 | if (IPCB(skb)->opt.optlen == 0) |
| 87 | return; |
| 88 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 89 | put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen, |
| 90 | ip_hdr(skb) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Denis V. Lunev | 7a6adb9 | 2008-03-24 15:30:27 -0700 | [diff] [blame] | 165 | int ip_cmsg_send(struct net *net, struct msghdr *msg, struct ipcm_cookie *ipc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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)); |
Denis V. Lunev | 7a6adb9 | 2008-03-24 15:30:27 -0700 | [diff] [blame] | 178 | err = ip_options_get(net, &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; |
Arnaldo Carvalho de Melo | 88c7664 | 2007-03-13 14:43:18 -0300 | [diff] [blame] | 271 | serr->ee.ee_type = icmp_hdr(skb)->type; |
| 272 | serr->ee.ee_code = icmp_hdr(skb)->code; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | serr->ee.ee_pad = 0; |
| 274 | serr->ee.ee_info = info; |
| 275 | serr->ee.ee_data = 0; |
Arnaldo Carvalho de Melo | 88c7664 | 2007-03-13 14:43:18 -0300 | [diff] [blame] | 276 | 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] | 277 | skb_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | serr->port = port; |
| 279 | |
Arnaldo Carvalho de Melo | bd82393 | 2007-03-13 17:10:43 -0300 | [diff] [blame] | 280 | if (skb_pull(skb, payload - skb->data) != NULL) { |
| 281 | skb_reset_transport_header(skb); |
| 282 | if (sock_queue_err_skb(sk, skb) == 0) |
| 283 | return; |
| 284 | } |
| 285 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Al Viro | 0579016 | 2006-09-27 18:33:40 -0700 | [diff] [blame] | 288 | 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] | 289 | { |
| 290 | struct inet_sock *inet = inet_sk(sk); |
| 291 | struct sock_exterr_skb *serr; |
| 292 | struct iphdr *iph; |
| 293 | struct sk_buff *skb; |
| 294 | |
| 295 | if (!inet->recverr) |
| 296 | return; |
| 297 | |
| 298 | skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC); |
| 299 | if (!skb) |
| 300 | return; |
| 301 | |
Arnaldo Carvalho de Melo | 2ca9e6f | 2007-03-10 19:15:25 -0300 | [diff] [blame] | 302 | skb_put(skb, sizeof(struct iphdr)); |
| 303 | skb_reset_network_header(skb); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 304 | iph = ip_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | iph->daddr = daddr; |
| 306 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 307 | serr = SKB_EXT_ERR(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | serr->ee.ee_errno = err; |
| 309 | serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 310 | serr->ee.ee_type = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | serr->ee.ee_code = 0; |
| 312 | serr->ee.ee_pad = 0; |
| 313 | serr->ee.ee_info = info; |
| 314 | serr->ee.ee_data = 0; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 315 | serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | serr->port = port; |
| 317 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 318 | __skb_pull(skb, skb_tail_pointer(skb) - skb->data); |
Arnaldo Carvalho de Melo | bd82393 | 2007-03-13 17:10:43 -0300 | [diff] [blame] | 319 | skb_reset_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | |
| 321 | if (sock_queue_err_skb(sk, skb)) |
| 322 | kfree_skb(skb); |
| 323 | } |
| 324 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 325 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | * Handle MSG_ERRQUEUE |
| 327 | */ |
| 328 | int ip_recv_error(struct sock *sk, struct msghdr *msg, int len) |
| 329 | { |
| 330 | struct sock_exterr_skb *serr; |
| 331 | struct sk_buff *skb, *skb2; |
| 332 | struct sockaddr_in *sin; |
| 333 | struct { |
| 334 | struct sock_extended_err ee; |
| 335 | struct sockaddr_in offender; |
| 336 | } errhdr; |
| 337 | int err; |
| 338 | int copied; |
| 339 | |
| 340 | err = -EAGAIN; |
| 341 | skb = skb_dequeue(&sk->sk_error_queue); |
| 342 | if (skb == NULL) |
| 343 | goto out; |
| 344 | |
| 345 | copied = skb->len; |
| 346 | if (copied > len) { |
| 347 | msg->msg_flags |= MSG_TRUNC; |
| 348 | copied = len; |
| 349 | } |
| 350 | err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); |
| 351 | if (err) |
| 352 | goto out_free_skb; |
| 353 | |
| 354 | sock_recv_timestamp(msg, sk, skb); |
| 355 | |
| 356 | serr = SKB_EXT_ERR(skb); |
| 357 | |
| 358 | sin = (struct sockaddr_in *)msg->msg_name; |
| 359 | if (sin) { |
| 360 | sin->sin_family = AF_INET; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 361 | sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) + |
| 362 | serr->addr_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | sin->sin_port = serr->port; |
| 364 | memset(&sin->sin_zero, 0, sizeof(sin->sin_zero)); |
| 365 | } |
| 366 | |
| 367 | memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err)); |
| 368 | sin = &errhdr.offender; |
| 369 | sin->sin_family = AF_UNSPEC; |
| 370 | if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP) { |
| 371 | struct inet_sock *inet = inet_sk(sk); |
| 372 | |
| 373 | sin->sin_family = AF_INET; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 374 | sin->sin_addr.s_addr = ip_hdr(skb)->saddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | sin->sin_port = 0; |
| 376 | memset(&sin->sin_zero, 0, sizeof(sin->sin_zero)); |
| 377 | if (inet->cmsg_flags) |
| 378 | ip_cmsg_recv(msg, skb); |
| 379 | } |
| 380 | |
| 381 | put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr); |
| 382 | |
| 383 | /* Now we could try to dump offended packet options */ |
| 384 | |
| 385 | msg->msg_flags |= MSG_ERRQUEUE; |
| 386 | err = copied; |
| 387 | |
| 388 | /* Reset and regenerate socket error */ |
Herbert Xu | e0f9f85 | 2005-06-18 22:56:18 -0700 | [diff] [blame] | 389 | spin_lock_bh(&sk->sk_error_queue.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | sk->sk_err = 0; |
| 391 | if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) { |
| 392 | sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno; |
Herbert Xu | e0f9f85 | 2005-06-18 22:56:18 -0700 | [diff] [blame] | 393 | spin_unlock_bh(&sk->sk_error_queue.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | sk->sk_error_report(sk); |
| 395 | } else |
Herbert Xu | e0f9f85 | 2005-06-18 22:56:18 -0700 | [diff] [blame] | 396 | spin_unlock_bh(&sk->sk_error_queue.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 398 | out_free_skb: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | kfree_skb(skb); |
| 400 | out: |
| 401 | return err; |
| 402 | } |
| 403 | |
| 404 | |
| 405 | /* |
| 406 | * Socket option code for IP. This is the end of the line after any TCP,UDP etc options on |
| 407 | * an IP socket. |
| 408 | */ |
| 409 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 410 | static int do_ip_setsockopt(struct sock *sk, int level, |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 411 | int optname, char __user *optval, int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | { |
| 413 | struct inet_sock *inet = inet_sk(sk); |
| 414 | int val=0,err; |
| 415 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 416 | if (((1<<optname) & ((1<<IP_PKTINFO) | (1<<IP_RECVTTL) | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 417 | (1<<IP_RECVOPTS) | (1<<IP_RECVTOS) | |
| 418 | (1<<IP_RETOPTS) | (1<<IP_TOS) | |
| 419 | (1<<IP_TTL) | (1<<IP_HDRINCL) | |
| 420 | (1<<IP_MTU_DISCOVER) | (1<<IP_RECVERR) | |
| 421 | (1<<IP_ROUTER_ALERT) | (1<<IP_FREEBIND) | |
KOVACS Krisztian | f5715ae | 2008-10-01 07:30:02 -0700 | [diff] [blame] | 422 | (1<<IP_PASSSEC) | (1<<IP_TRANSPARENT))) || |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 423 | optname == IP_MULTICAST_TTL || |
| 424 | optname == IP_MULTICAST_LOOP) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | if (optlen >= sizeof(int)) { |
| 426 | if (get_user(val, (int __user *) optval)) |
| 427 | return -EFAULT; |
| 428 | } else if (optlen >= sizeof(char)) { |
| 429 | unsigned char ucval; |
| 430 | |
| 431 | if (get_user(ucval, (unsigned char __user *) optval)) |
| 432 | return -EFAULT; |
| 433 | val = (int) ucval; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | /* If optlen==0, it is equivalent to val == 0 */ |
| 438 | |
Pavel Emelyanov | 6a9fb94 | 2007-11-05 21:32:31 -0800 | [diff] [blame] | 439 | if (ip_mroute_opt(optname)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | return ip_mroute_setsockopt(sk,optname,optval,optlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | |
| 442 | err = 0; |
| 443 | lock_sock(sk); |
| 444 | |
| 445 | switch (optname) { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 446 | case IP_OPTIONS: |
| 447 | { |
| 448 | struct ip_options * opt = NULL; |
| 449 | if (optlen > 40 || optlen < 0) |
| 450 | goto e_inval; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 451 | err = ip_options_get_from_user(sock_net(sk), &opt, |
Denis V. Lunev | cb84663 | 2008-03-24 15:31:00 -0700 | [diff] [blame] | 452 | optval, optlen); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 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 (inet->tos != val) { |
| 518 | inet->tos = val; |
| 519 | sk->sk_priority = rt_tos2priority(val); |
| 520 | sk_dst_reset(sk); |
| 521 | } |
| 522 | break; |
| 523 | case IP_TTL: |
| 524 | if (optlen<1) |
| 525 | goto e_inval; |
| 526 | if (val != -1 && (val < 1 || val>255)) |
| 527 | goto e_inval; |
| 528 | inet->uc_ttl = val; |
| 529 | break; |
| 530 | case IP_HDRINCL: |
| 531 | if (sk->sk_type != SOCK_RAW) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | err = -ENOPROTOOPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 534 | } |
| 535 | inet->hdrincl = val ? 1 : 0; |
| 536 | break; |
| 537 | case IP_MTU_DISCOVER: |
John Heffner | 628a5c5 | 2007-04-20 15:53:27 -0700 | [diff] [blame] | 538 | if (val<0 || val>3) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 539 | goto e_inval; |
| 540 | inet->pmtudisc = val; |
| 541 | break; |
| 542 | case IP_RECVERR: |
| 543 | inet->recverr = !!val; |
| 544 | if (!val) |
| 545 | skb_queue_purge(&sk->sk_error_queue); |
| 546 | break; |
| 547 | case IP_MULTICAST_TTL: |
| 548 | if (sk->sk_type == SOCK_STREAM) |
| 549 | goto e_inval; |
| 550 | if (optlen<1) |
| 551 | goto e_inval; |
| 552 | if (val==-1) |
| 553 | val = 1; |
| 554 | if (val < 0 || val > 255) |
| 555 | goto e_inval; |
| 556 | inet->mc_ttl = val; |
| 557 | break; |
| 558 | case IP_MULTICAST_LOOP: |
| 559 | if (optlen<1) |
| 560 | goto e_inval; |
| 561 | inet->mc_loop = !!val; |
| 562 | break; |
| 563 | case IP_MULTICAST_IF: |
| 564 | { |
| 565 | struct ip_mreqn mreq; |
| 566 | struct net_device *dev = NULL; |
| 567 | |
| 568 | if (sk->sk_type == SOCK_STREAM) |
| 569 | goto e_inval; |
| 570 | /* |
| 571 | * Check the arguments are allowable |
| 572 | */ |
| 573 | |
| 574 | err = -EFAULT; |
| 575 | if (optlen >= sizeof(struct ip_mreqn)) { |
| 576 | if (copy_from_user(&mreq,optval,sizeof(mreq))) |
| 577 | break; |
| 578 | } else { |
| 579 | memset(&mreq, 0, sizeof(mreq)); |
| 580 | if (optlen >= sizeof(struct in_addr) && |
| 581 | copy_from_user(&mreq.imr_address,optval,sizeof(struct in_addr))) |
| 582 | break; |
| 583 | } |
| 584 | |
| 585 | if (!mreq.imr_ifindex) { |
Al Viro | e6f1ceb | 2008-03-17 22:44:53 -0700 | [diff] [blame] | 586 | if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 587 | inet->mc_index = 0; |
| 588 | inet->mc_addr = 0; |
| 589 | err = 0; |
| 590 | break; |
| 591 | } |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 592 | dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 593 | if (dev) { |
| 594 | mreq.imr_ifindex = dev->ifindex; |
| 595 | dev_put(dev); |
| 596 | } |
| 597 | } else |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 598 | dev = __dev_get_by_index(sock_net(sk), mreq.imr_ifindex); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 599 | |
| 600 | |
| 601 | err = -EADDRNOTAVAIL; |
| 602 | if (!dev) |
| 603 | break; |
| 604 | |
| 605 | err = -EINVAL; |
| 606 | if (sk->sk_bound_dev_if && |
| 607 | mreq.imr_ifindex != sk->sk_bound_dev_if) |
| 608 | break; |
| 609 | |
| 610 | inet->mc_index = mreq.imr_ifindex; |
| 611 | inet->mc_addr = mreq.imr_address.s_addr; |
| 612 | err = 0; |
| 613 | break; |
| 614 | } |
| 615 | |
| 616 | case IP_ADD_MEMBERSHIP: |
| 617 | case IP_DROP_MEMBERSHIP: |
| 618 | { |
| 619 | struct ip_mreqn mreq; |
| 620 | |
Flavio Leitner | a96fb49 | 2007-08-24 22:16:39 -0700 | [diff] [blame] | 621 | err = -EPROTO; |
| 622 | if (inet_sk(sk)->is_icsk) |
| 623 | break; |
| 624 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 625 | if (optlen < sizeof(struct ip_mreq)) |
| 626 | goto e_inval; |
| 627 | err = -EFAULT; |
| 628 | if (optlen >= sizeof(struct ip_mreqn)) { |
| 629 | if (copy_from_user(&mreq,optval,sizeof(mreq))) |
| 630 | break; |
| 631 | } else { |
| 632 | memset(&mreq, 0, sizeof(mreq)); |
| 633 | if (copy_from_user(&mreq,optval,sizeof(struct ip_mreq))) |
| 634 | break; |
| 635 | } |
| 636 | |
| 637 | if (optname == IP_ADD_MEMBERSHIP) |
| 638 | err = ip_mc_join_group(sk, &mreq); |
| 639 | else |
| 640 | err = ip_mc_leave_group(sk, &mreq); |
| 641 | break; |
| 642 | } |
| 643 | case IP_MSFILTER: |
| 644 | { |
| 645 | extern int sysctl_igmp_max_msf; |
| 646 | struct ip_msfilter *msf; |
| 647 | |
| 648 | if (optlen < IP_MSFILTER_SIZE(0)) |
| 649 | goto e_inval; |
| 650 | if (optlen > sysctl_optmem_max) { |
| 651 | err = -ENOBUFS; |
| 652 | break; |
| 653 | } |
| 654 | msf = kmalloc(optlen, GFP_KERNEL); |
Stephen Hemminger | cfcabdc | 2007-10-09 01:59:42 -0700 | [diff] [blame] | 655 | if (!msf) { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 656 | err = -ENOBUFS; |
| 657 | break; |
| 658 | } |
| 659 | err = -EFAULT; |
| 660 | if (copy_from_user(msf, optval, optlen)) { |
| 661 | kfree(msf); |
| 662 | break; |
| 663 | } |
| 664 | /* numsrc >= (1G-4) overflow in 32 bits */ |
| 665 | if (msf->imsf_numsrc >= 0x3ffffffcU || |
| 666 | msf->imsf_numsrc > sysctl_igmp_max_msf) { |
| 667 | kfree(msf); |
| 668 | err = -ENOBUFS; |
| 669 | break; |
| 670 | } |
| 671 | if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) { |
| 672 | kfree(msf); |
| 673 | err = -EINVAL; |
| 674 | break; |
| 675 | } |
| 676 | err = ip_mc_msfilter(sk, msf, 0); |
| 677 | kfree(msf); |
| 678 | break; |
| 679 | } |
| 680 | case IP_BLOCK_SOURCE: |
| 681 | case IP_UNBLOCK_SOURCE: |
| 682 | case IP_ADD_SOURCE_MEMBERSHIP: |
| 683 | case IP_DROP_SOURCE_MEMBERSHIP: |
| 684 | { |
| 685 | struct ip_mreq_source mreqs; |
| 686 | int omode, add; |
| 687 | |
| 688 | if (optlen != sizeof(struct ip_mreq_source)) |
| 689 | goto e_inval; |
| 690 | if (copy_from_user(&mreqs, optval, sizeof(mreqs))) { |
| 691 | err = -EFAULT; |
| 692 | break; |
| 693 | } |
| 694 | if (optname == IP_BLOCK_SOURCE) { |
| 695 | omode = MCAST_EXCLUDE; |
| 696 | add = 1; |
| 697 | } else if (optname == IP_UNBLOCK_SOURCE) { |
| 698 | omode = MCAST_EXCLUDE; |
| 699 | add = 0; |
| 700 | } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) { |
| 701 | struct ip_mreqn mreq; |
| 702 | |
| 703 | mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr; |
| 704 | mreq.imr_address.s_addr = mreqs.imr_interface; |
| 705 | mreq.imr_ifindex = 0; |
| 706 | err = ip_mc_join_group(sk, &mreq); |
| 707 | if (err && err != -EADDRINUSE) |
| 708 | break; |
| 709 | omode = MCAST_INCLUDE; |
| 710 | add = 1; |
| 711 | } else /* IP_DROP_SOURCE_MEMBERSHIP */ { |
| 712 | omode = MCAST_INCLUDE; |
| 713 | add = 0; |
| 714 | } |
| 715 | err = ip_mc_source(add, omode, sk, &mreqs, 0); |
| 716 | break; |
| 717 | } |
| 718 | case MCAST_JOIN_GROUP: |
| 719 | case MCAST_LEAVE_GROUP: |
| 720 | { |
| 721 | struct group_req greq; |
| 722 | struct sockaddr_in *psin; |
| 723 | struct ip_mreqn mreq; |
| 724 | |
| 725 | if (optlen < sizeof(struct group_req)) |
| 726 | goto e_inval; |
| 727 | err = -EFAULT; |
| 728 | if (copy_from_user(&greq, optval, sizeof(greq))) |
| 729 | break; |
| 730 | psin = (struct sockaddr_in *)&greq.gr_group; |
| 731 | if (psin->sin_family != AF_INET) |
| 732 | goto e_inval; |
| 733 | memset(&mreq, 0, sizeof(mreq)); |
| 734 | mreq.imr_multiaddr = psin->sin_addr; |
| 735 | mreq.imr_ifindex = greq.gr_interface; |
| 736 | |
| 737 | if (optname == MCAST_JOIN_GROUP) |
| 738 | err = ip_mc_join_group(sk, &mreq); |
| 739 | else |
| 740 | err = ip_mc_leave_group(sk, &mreq); |
| 741 | break; |
| 742 | } |
| 743 | case MCAST_JOIN_SOURCE_GROUP: |
| 744 | case MCAST_LEAVE_SOURCE_GROUP: |
| 745 | case MCAST_BLOCK_SOURCE: |
| 746 | case MCAST_UNBLOCK_SOURCE: |
| 747 | { |
| 748 | struct group_source_req greqs; |
| 749 | struct ip_mreq_source mreqs; |
| 750 | struct sockaddr_in *psin; |
| 751 | int omode, add; |
| 752 | |
| 753 | if (optlen != sizeof(struct group_source_req)) |
| 754 | goto e_inval; |
| 755 | if (copy_from_user(&greqs, optval, sizeof(greqs))) { |
| 756 | err = -EFAULT; |
| 757 | break; |
| 758 | } |
| 759 | if (greqs.gsr_group.ss_family != AF_INET || |
| 760 | greqs.gsr_source.ss_family != AF_INET) { |
| 761 | err = -EADDRNOTAVAIL; |
| 762 | break; |
| 763 | } |
| 764 | psin = (struct sockaddr_in *)&greqs.gsr_group; |
| 765 | mreqs.imr_multiaddr = psin->sin_addr.s_addr; |
| 766 | psin = (struct sockaddr_in *)&greqs.gsr_source; |
| 767 | mreqs.imr_sourceaddr = psin->sin_addr.s_addr; |
| 768 | mreqs.imr_interface = 0; /* use index for mc_source */ |
| 769 | |
| 770 | if (optname == MCAST_BLOCK_SOURCE) { |
| 771 | omode = MCAST_EXCLUDE; |
| 772 | add = 1; |
| 773 | } else if (optname == MCAST_UNBLOCK_SOURCE) { |
| 774 | omode = MCAST_EXCLUDE; |
| 775 | add = 0; |
| 776 | } else if (optname == MCAST_JOIN_SOURCE_GROUP) { |
| 777 | struct ip_mreqn mreq; |
| 778 | |
| 779 | psin = (struct sockaddr_in *)&greqs.gsr_group; |
| 780 | mreq.imr_multiaddr = psin->sin_addr; |
| 781 | mreq.imr_address.s_addr = 0; |
| 782 | mreq.imr_ifindex = greqs.gsr_interface; |
| 783 | err = ip_mc_join_group(sk, &mreq); |
| 784 | if (err && err != -EADDRINUSE) |
| 785 | break; |
| 786 | greqs.gsr_interface = mreq.imr_ifindex; |
| 787 | omode = MCAST_INCLUDE; |
| 788 | add = 1; |
| 789 | } else /* MCAST_LEAVE_SOURCE_GROUP */ { |
| 790 | omode = MCAST_INCLUDE; |
| 791 | add = 0; |
| 792 | } |
| 793 | err = ip_mc_source(add, omode, sk, &mreqs, |
| 794 | greqs.gsr_interface); |
| 795 | break; |
| 796 | } |
| 797 | case MCAST_MSFILTER: |
| 798 | { |
| 799 | extern int sysctl_igmp_max_msf; |
| 800 | struct sockaddr_in *psin; |
| 801 | struct ip_msfilter *msf = NULL; |
| 802 | struct group_filter *gsf = NULL; |
| 803 | int msize, i, ifindex; |
| 804 | |
| 805 | if (optlen < GROUP_FILTER_SIZE(0)) |
| 806 | goto e_inval; |
| 807 | if (optlen > sysctl_optmem_max) { |
| 808 | err = -ENOBUFS; |
| 809 | break; |
| 810 | } |
| 811 | gsf = kmalloc(optlen,GFP_KERNEL); |
Stephen Hemminger | cfcabdc | 2007-10-09 01:59:42 -0700 | [diff] [blame] | 812 | if (!gsf) { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 813 | err = -ENOBUFS; |
| 814 | break; |
| 815 | } |
| 816 | err = -EFAULT; |
| 817 | if (copy_from_user(gsf, optval, optlen)) { |
| 818 | goto mc_msf_out; |
| 819 | } |
| 820 | /* numsrc >= (4G-140)/128 overflow in 32 bits */ |
| 821 | if (gsf->gf_numsrc >= 0x1ffffff || |
| 822 | gsf->gf_numsrc > sysctl_igmp_max_msf) { |
| 823 | err = -ENOBUFS; |
| 824 | goto mc_msf_out; |
| 825 | } |
| 826 | if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) { |
| 827 | err = -EINVAL; |
| 828 | goto mc_msf_out; |
| 829 | } |
| 830 | msize = IP_MSFILTER_SIZE(gsf->gf_numsrc); |
| 831 | msf = kmalloc(msize,GFP_KERNEL); |
Stephen Hemminger | cfcabdc | 2007-10-09 01:59:42 -0700 | [diff] [blame] | 832 | if (!msf) { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 833 | err = -ENOBUFS; |
| 834 | goto mc_msf_out; |
| 835 | } |
| 836 | ifindex = gsf->gf_interface; |
| 837 | psin = (struct sockaddr_in *)&gsf->gf_group; |
| 838 | if (psin->sin_family != AF_INET) { |
| 839 | err = -EADDRNOTAVAIL; |
| 840 | goto mc_msf_out; |
| 841 | } |
| 842 | msf->imsf_multiaddr = psin->sin_addr.s_addr; |
| 843 | msf->imsf_interface = 0; |
| 844 | msf->imsf_fmode = gsf->gf_fmode; |
| 845 | msf->imsf_numsrc = gsf->gf_numsrc; |
| 846 | err = -EADDRNOTAVAIL; |
| 847 | for (i=0; i<gsf->gf_numsrc; ++i) { |
| 848 | psin = (struct sockaddr_in *)&gsf->gf_slist[i]; |
| 849 | |
| 850 | if (psin->sin_family != AF_INET) |
| 851 | goto mc_msf_out; |
| 852 | msf->imsf_slist[i] = psin->sin_addr.s_addr; |
| 853 | } |
| 854 | kfree(gsf); |
| 855 | gsf = NULL; |
| 856 | |
| 857 | err = ip_mc_msfilter(sk, msf, ifindex); |
| 858 | mc_msf_out: |
| 859 | kfree(msf); |
| 860 | kfree(gsf); |
| 861 | break; |
| 862 | } |
| 863 | case IP_ROUTER_ALERT: |
| 864 | err = ip_ra_control(sk, val ? 1 : 0, NULL); |
| 865 | break; |
| 866 | |
| 867 | case IP_FREEBIND: |
| 868 | if (optlen<1) |
| 869 | goto e_inval; |
| 870 | inet->freebind = !!val; |
| 871 | break; |
| 872 | |
| 873 | case IP_IPSEC_POLICY: |
| 874 | case IP_XFRM_POLICY: |
| 875 | err = -EPERM; |
| 876 | if (!capable(CAP_NET_ADMIN)) |
| 877 | break; |
| 878 | err = xfrm_user_policy(sk, optname, optval, optlen); |
| 879 | break; |
| 880 | |
KOVACS Krisztian | f5715ae | 2008-10-01 07:30:02 -0700 | [diff] [blame] | 881 | case IP_TRANSPARENT: |
| 882 | if (!capable(CAP_NET_ADMIN)) { |
| 883 | err = -EPERM; |
| 884 | break; |
| 885 | } |
| 886 | if (optlen < 1) |
| 887 | goto e_inval; |
| 888 | inet->transparent = !!val; |
| 889 | break; |
| 890 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 891 | default: |
| 892 | err = -ENOPROTOOPT; |
| 893 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | } |
| 895 | release_sock(sk); |
| 896 | return err; |
| 897 | |
| 898 | e_inval: |
| 899 | release_sock(sk); |
| 900 | return -EINVAL; |
| 901 | } |
| 902 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 903 | int ip_setsockopt(struct sock *sk, int level, |
| 904 | int optname, char __user *optval, int optlen) |
| 905 | { |
| 906 | int err; |
| 907 | |
| 908 | if (level != SOL_IP) |
| 909 | return -ENOPROTOOPT; |
| 910 | |
| 911 | err = do_ip_setsockopt(sk, level, optname, optval, optlen); |
| 912 | #ifdef CONFIG_NETFILTER |
| 913 | /* we need to exclude all possible ENOPROTOOPTs except default case */ |
| 914 | if (err == -ENOPROTOOPT && optname != IP_HDRINCL && |
Pavel Emelyanov | 6a9fb94 | 2007-11-05 21:32:31 -0800 | [diff] [blame] | 915 | optname != IP_IPSEC_POLICY && |
| 916 | optname != IP_XFRM_POLICY && |
| 917 | !ip_mroute_opt(optname)) { |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 918 | lock_sock(sk); |
| 919 | err = nf_setsockopt(sk, PF_INET, optname, optval, optlen); |
| 920 | release_sock(sk); |
| 921 | } |
| 922 | #endif |
| 923 | return err; |
| 924 | } |
| 925 | |
| 926 | #ifdef CONFIG_COMPAT |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 927 | int compat_ip_setsockopt(struct sock *sk, int level, int optname, |
| 928 | char __user *optval, int optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 929 | { |
| 930 | int err; |
| 931 | |
| 932 | if (level != SOL_IP) |
| 933 | return -ENOPROTOOPT; |
| 934 | |
David L Stevens | dae5029 | 2008-04-27 01:06:07 -0700 | [diff] [blame] | 935 | if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER) |
| 936 | return compat_mc_setsockopt(sk, level, optname, optval, optlen, |
| 937 | ip_setsockopt); |
| 938 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 939 | err = do_ip_setsockopt(sk, level, optname, optval, optlen); |
| 940 | #ifdef CONFIG_NETFILTER |
| 941 | /* we need to exclude all possible ENOPROTOOPTs except default case */ |
| 942 | if (err == -ENOPROTOOPT && optname != IP_HDRINCL && |
Pavel Emelyanov | 6a9fb94 | 2007-11-05 21:32:31 -0800 | [diff] [blame] | 943 | optname != IP_IPSEC_POLICY && |
| 944 | optname != IP_XFRM_POLICY && |
| 945 | !ip_mroute_opt(optname)) { |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 946 | lock_sock(sk); |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 947 | err = compat_nf_setsockopt(sk, PF_INET, optname, |
| 948 | optval, optlen); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 949 | release_sock(sk); |
| 950 | } |
| 951 | #endif |
| 952 | return err; |
| 953 | } |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 954 | |
| 955 | EXPORT_SYMBOL(compat_ip_setsockopt); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 956 | #endif |
| 957 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | /* |
| 959 | * Get the options. Note for future reference. The GET of IP options gets the |
| 960 | * _received_ ones. The set sets the _sent_ ones. |
| 961 | */ |
| 962 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 963 | static int do_ip_getsockopt(struct sock *sk, int level, int optname, |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 964 | char __user *optval, int __user *optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | { |
| 966 | struct inet_sock *inet = inet_sk(sk); |
| 967 | int val; |
| 968 | int len; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 969 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 970 | if (level != SOL_IP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | return -EOPNOTSUPP; |
| 972 | |
Pavel Emelyanov | 6a9fb94 | 2007-11-05 21:32:31 -0800 | [diff] [blame] | 973 | if (ip_mroute_opt(optname)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | return ip_mroute_getsockopt(sk,optname,optval,optlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 976 | if (get_user(len,optlen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | return -EFAULT; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 978 | if (len < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | return -EINVAL; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 980 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | lock_sock(sk); |
| 982 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 983 | switch (optname) { |
| 984 | case IP_OPTIONS: |
| 985 | { |
| 986 | unsigned char optbuf[sizeof(struct ip_options)+40]; |
| 987 | struct ip_options * opt = (struct ip_options*)optbuf; |
| 988 | opt->optlen = 0; |
| 989 | if (inet->opt) |
| 990 | memcpy(optbuf, inet->opt, |
| 991 | sizeof(struct ip_options)+ |
| 992 | inet->opt->optlen); |
| 993 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 995 | if (opt->optlen == 0) |
| 996 | return put_user(0, optlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 998 | ip_options_undo(opt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1000 | len = min_t(unsigned int, len, opt->optlen); |
| 1001 | if (put_user(len, optlen)) |
| 1002 | return -EFAULT; |
| 1003 | if (copy_to_user(optval, opt->__data, len)) |
| 1004 | return -EFAULT; |
| 1005 | return 0; |
| 1006 | } |
| 1007 | case IP_PKTINFO: |
| 1008 | val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0; |
| 1009 | break; |
| 1010 | case IP_RECVTTL: |
| 1011 | val = (inet->cmsg_flags & IP_CMSG_TTL) != 0; |
| 1012 | break; |
| 1013 | case IP_RECVTOS: |
| 1014 | val = (inet->cmsg_flags & IP_CMSG_TOS) != 0; |
| 1015 | break; |
| 1016 | case IP_RECVOPTS: |
| 1017 | val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0; |
| 1018 | break; |
| 1019 | case IP_RETOPTS: |
| 1020 | val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0; |
| 1021 | break; |
| 1022 | case IP_PASSSEC: |
| 1023 | val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0; |
| 1024 | break; |
| 1025 | case IP_TOS: |
| 1026 | val = inet->tos; |
| 1027 | break; |
| 1028 | case IP_TTL: |
| 1029 | val = (inet->uc_ttl == -1 ? |
| 1030 | sysctl_ip_default_ttl : |
| 1031 | inet->uc_ttl); |
| 1032 | break; |
| 1033 | case IP_HDRINCL: |
| 1034 | val = inet->hdrincl; |
| 1035 | break; |
| 1036 | case IP_MTU_DISCOVER: |
| 1037 | val = inet->pmtudisc; |
| 1038 | break; |
| 1039 | case IP_MTU: |
| 1040 | { |
| 1041 | struct dst_entry *dst; |
| 1042 | val = 0; |
| 1043 | dst = sk_dst_get(sk); |
| 1044 | if (dst) { |
| 1045 | val = dst_mtu(dst); |
| 1046 | dst_release(dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1048 | if (!val) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | release_sock(sk); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1050 | return -ENOTCONN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1052 | break; |
| 1053 | } |
| 1054 | case IP_RECVERR: |
| 1055 | val = inet->recverr; |
| 1056 | break; |
| 1057 | case IP_MULTICAST_TTL: |
| 1058 | val = inet->mc_ttl; |
| 1059 | break; |
| 1060 | case IP_MULTICAST_LOOP: |
| 1061 | val = inet->mc_loop; |
| 1062 | break; |
| 1063 | case IP_MULTICAST_IF: |
| 1064 | { |
| 1065 | struct in_addr addr; |
| 1066 | len = min_t(unsigned int, len, sizeof(struct in_addr)); |
| 1067 | addr.s_addr = inet->mc_addr; |
| 1068 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1070 | if (put_user(len, optlen)) |
| 1071 | return -EFAULT; |
| 1072 | if (copy_to_user(optval, &addr, len)) |
| 1073 | return -EFAULT; |
| 1074 | return 0; |
| 1075 | } |
| 1076 | case IP_MSFILTER: |
| 1077 | { |
| 1078 | struct ip_msfilter msf; |
| 1079 | int err; |
| 1080 | |
| 1081 | if (len < IP_MSFILTER_SIZE(0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | release_sock(sk); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1083 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1085 | if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | release_sock(sk); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1087 | return -EFAULT; |
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 | err = ip_mc_msfget(sk, &msf, |
| 1090 | (struct ip_msfilter __user *)optval, optlen); |
| 1091 | release_sock(sk); |
| 1092 | return err; |
| 1093 | } |
| 1094 | case MCAST_MSFILTER: |
| 1095 | { |
| 1096 | struct group_filter gsf; |
| 1097 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1099 | if (len < GROUP_FILTER_SIZE(0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | release_sock(sk); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1101 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1103 | if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | release_sock(sk); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1105 | return -EFAULT; |
| 1106 | } |
| 1107 | err = ip_mc_gsfget(sk, &gsf, |
| 1108 | (struct group_filter __user *)optval, optlen); |
| 1109 | release_sock(sk); |
| 1110 | return err; |
| 1111 | } |
| 1112 | case IP_PKTOPTIONS: |
| 1113 | { |
| 1114 | struct msghdr msg; |
| 1115 | |
| 1116 | release_sock(sk); |
| 1117 | |
| 1118 | if (sk->sk_type != SOCK_STREAM) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | return -ENOPROTOOPT; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1120 | |
| 1121 | msg.msg_control = optval; |
| 1122 | msg.msg_controllen = len; |
| 1123 | msg.msg_flags = 0; |
| 1124 | |
| 1125 | if (inet->cmsg_flags & IP_CMSG_PKTINFO) { |
| 1126 | struct in_pktinfo info; |
| 1127 | |
| 1128 | info.ipi_addr.s_addr = inet->rcv_saddr; |
| 1129 | info.ipi_spec_dst.s_addr = inet->rcv_saddr; |
| 1130 | info.ipi_ifindex = inet->mc_index; |
| 1131 | put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info); |
| 1132 | } |
| 1133 | if (inet->cmsg_flags & IP_CMSG_TTL) { |
| 1134 | int hlim = inet->mc_ttl; |
| 1135 | put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim); |
| 1136 | } |
| 1137 | len -= msg.msg_controllen; |
| 1138 | return put_user(len, optlen); |
| 1139 | } |
| 1140 | case IP_FREEBIND: |
| 1141 | val = inet->freebind; |
| 1142 | break; |
KOVACS Krisztian | f5715ae | 2008-10-01 07:30:02 -0700 | [diff] [blame] | 1143 | case IP_TRANSPARENT: |
| 1144 | val = inet->transparent; |
| 1145 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1146 | default: |
| 1147 | release_sock(sk); |
| 1148 | return -ENOPROTOOPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | } |
| 1150 | release_sock(sk); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1151 | |
David S. Miller | 951e07c | 2008-04-10 01:29:36 -0700 | [diff] [blame] | 1152 | if (len < sizeof(int) && len > 0 && val>=0 && val<=255) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | unsigned char ucval = (unsigned char)val; |
| 1154 | len = 1; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1155 | if (put_user(len, optlen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | return -EFAULT; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1157 | if (copy_to_user(optval,&ucval,1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | return -EFAULT; |
| 1159 | } else { |
| 1160 | len = min_t(unsigned int, sizeof(int), len); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1161 | if (put_user(len, optlen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | return -EFAULT; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1163 | if (copy_to_user(optval,&val,len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | return -EFAULT; |
| 1165 | } |
| 1166 | return 0; |
| 1167 | } |
| 1168 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1169 | int ip_getsockopt(struct sock *sk, int level, |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1170 | int optname, char __user *optval, int __user *optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1171 | { |
| 1172 | int err; |
| 1173 | |
| 1174 | err = do_ip_getsockopt(sk, level, optname, optval, optlen); |
| 1175 | #ifdef CONFIG_NETFILTER |
| 1176 | /* we need to exclude all possible ENOPROTOOPTs except default case */ |
Pavel Emelyanov | 6a9fb94 | 2007-11-05 21:32:31 -0800 | [diff] [blame] | 1177 | if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS && |
| 1178 | !ip_mroute_opt(optname)) { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1179 | int len; |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1180 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1181 | if (get_user(len,optlen)) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1182 | return -EFAULT; |
| 1183 | |
| 1184 | lock_sock(sk); |
| 1185 | err = nf_getsockopt(sk, PF_INET, optname, optval, |
| 1186 | &len); |
| 1187 | release_sock(sk); |
| 1188 | if (err >= 0) |
| 1189 | err = put_user(len, optlen); |
| 1190 | return err; |
| 1191 | } |
| 1192 | #endif |
| 1193 | return err; |
| 1194 | } |
| 1195 | |
| 1196 | #ifdef CONFIG_COMPAT |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1197 | int compat_ip_getsockopt(struct sock *sk, int level, int optname, |
| 1198 | char __user *optval, int __user *optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1199 | { |
David L Stevens | 42908c6 | 2008-04-29 03:23:22 -0700 | [diff] [blame] | 1200 | int err; |
| 1201 | |
| 1202 | if (optname == MCAST_MSFILTER) |
| 1203 | return compat_mc_getsockopt(sk, level, optname, optval, optlen, |
| 1204 | ip_getsockopt); |
| 1205 | |
| 1206 | err = do_ip_getsockopt(sk, level, optname, optval, optlen); |
| 1207 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1208 | #ifdef CONFIG_NETFILTER |
| 1209 | /* we need to exclude all possible ENOPROTOOPTs except default case */ |
Pavel Emelyanov | 6a9fb94 | 2007-11-05 21:32:31 -0800 | [diff] [blame] | 1210 | if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS && |
| 1211 | !ip_mroute_opt(optname)) { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1212 | int len; |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1213 | |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1214 | if (get_user(len, optlen)) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1215 | return -EFAULT; |
| 1216 | |
| 1217 | lock_sock(sk); |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1218 | err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1219 | release_sock(sk); |
| 1220 | if (err >= 0) |
| 1221 | err = put_user(len, optlen); |
| 1222 | return err; |
| 1223 | } |
| 1224 | #endif |
| 1225 | return err; |
| 1226 | } |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1227 | |
| 1228 | EXPORT_SYMBOL(compat_ip_getsockopt); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1229 | #endif |
| 1230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | EXPORT_SYMBOL(ip_cmsg_recv); |
| 1232 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | EXPORT_SYMBOL(ip_getsockopt); |
| 1234 | EXPORT_SYMBOL(ip_setsockopt); |