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