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