datagram.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  1. /*
  2. * common UDP/RAW code
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. */
  13. #include <linux/capability.h>
  14. #include <linux/errno.h>
  15. #include <linux/types.h>
  16. #include <linux/kernel.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/socket.h>
  19. #include <linux/sockios.h>
  20. #include <linux/in6.h>
  21. #include <linux/ipv6.h>
  22. #include <linux/route.h>
  23. #include <linux/slab.h>
  24. #include <linux/export.h>
  25. #include <net/ipv6.h>
  26. #include <net/ndisc.h>
  27. #include <net/addrconf.h>
  28. #include <net/transp_v6.h>
  29. #include <net/ip6_route.h>
  30. #include <net/tcp_states.h>
  31. #include <net/dsfield.h>
  32. #include <net/sock_reuseport.h>
  33. #include <linux/errqueue.h>
  34. #include <linux/uaccess.h>
  35. static bool ipv6_mapped_addr_any(const struct in6_addr *a)
  36. {
  37. return ipv6_addr_v4mapped(a) && (a->s6_addr32[3] == 0);
  38. }
  39. static void ip6_datagram_flow_key_init(struct flowi6 *fl6, struct sock *sk)
  40. {
  41. struct inet_sock *inet = inet_sk(sk);
  42. struct ipv6_pinfo *np = inet6_sk(sk);
  43. memset(fl6, 0, sizeof(*fl6));
  44. fl6->flowi6_proto = sk->sk_protocol;
  45. fl6->daddr = sk->sk_v6_daddr;
  46. fl6->saddr = np->saddr;
  47. fl6->flowi6_oif = sk->sk_bound_dev_if;
  48. fl6->flowi6_mark = sk->sk_mark;
  49. fl6->fl6_dport = inet->inet_dport;
  50. fl6->fl6_sport = inet->inet_sport;
  51. fl6->flowlabel = np->flow_label;
  52. fl6->flowi6_uid = sk->sk_uid;
  53. if (!fl6->flowi6_oif)
  54. fl6->flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
  55. if (!fl6->flowi6_oif && ipv6_addr_is_multicast(&fl6->daddr))
  56. fl6->flowi6_oif = np->mcast_oif;
  57. security_sk_classify_flow(sk, flowi6_to_flowi(fl6));
  58. }
  59. int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr)
  60. {
  61. struct ip6_flowlabel *flowlabel = NULL;
  62. struct in6_addr *final_p, final;
  63. struct ipv6_txoptions *opt;
  64. struct dst_entry *dst;
  65. struct inet_sock *inet = inet_sk(sk);
  66. struct ipv6_pinfo *np = inet6_sk(sk);
  67. struct flowi6 fl6;
  68. int err = 0;
  69. if (np->sndflow && (np->flow_label & IPV6_FLOWLABEL_MASK)) {
  70. flowlabel = fl6_sock_lookup(sk, np->flow_label);
  71. if (!flowlabel)
  72. return -EINVAL;
  73. }
  74. ip6_datagram_flow_key_init(&fl6, sk);
  75. rcu_read_lock();
  76. opt = flowlabel ? flowlabel->opt : rcu_dereference(np->opt);
  77. final_p = fl6_update_dst(&fl6, opt, &final);
  78. rcu_read_unlock();
  79. dst = ip6_dst_lookup_flow(sk, &fl6, final_p);
  80. if (IS_ERR(dst)) {
  81. err = PTR_ERR(dst);
  82. goto out;
  83. }
  84. if (fix_sk_saddr) {
  85. if (ipv6_addr_any(&np->saddr))
  86. np->saddr = fl6.saddr;
  87. if (ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
  88. sk->sk_v6_rcv_saddr = fl6.saddr;
  89. inet->inet_rcv_saddr = LOOPBACK4_IPV6;
  90. if (sk->sk_prot->rehash)
  91. sk->sk_prot->rehash(sk);
  92. }
  93. }
  94. ip6_sk_dst_store_flow(sk, dst, &fl6);
  95. out:
  96. fl6_sock_release(flowlabel);
  97. return err;
  98. }
  99. void ip6_datagram_release_cb(struct sock *sk)
  100. {
  101. struct dst_entry *dst;
  102. if (ipv6_addr_v4mapped(&sk->sk_v6_daddr))
  103. return;
  104. rcu_read_lock();
  105. dst = __sk_dst_get(sk);
  106. if (!dst || !dst->obsolete ||
  107. dst->ops->check(dst, inet6_sk(sk)->dst_cookie)) {
  108. rcu_read_unlock();
  109. return;
  110. }
  111. rcu_read_unlock();
  112. ip6_datagram_dst_update(sk, false);
  113. }
  114. EXPORT_SYMBOL_GPL(ip6_datagram_release_cb);
  115. int __ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr,
  116. int addr_len)
  117. {
  118. struct sockaddr_in6 *usin = (struct sockaddr_in6 *) uaddr;
  119. struct inet_sock *inet = inet_sk(sk);
  120. struct ipv6_pinfo *np = inet6_sk(sk);
  121. struct in6_addr *daddr, old_daddr;
  122. __be32 fl6_flowlabel = 0;
  123. __be32 old_fl6_flowlabel;
  124. __be16 old_dport;
  125. int addr_type;
  126. int err;
  127. if (usin->sin6_family == AF_INET) {
  128. if (__ipv6_only_sock(sk))
  129. return -EAFNOSUPPORT;
  130. err = __ip4_datagram_connect(sk, uaddr, addr_len);
  131. goto ipv4_connected;
  132. }
  133. if (addr_len < SIN6_LEN_RFC2133)
  134. return -EINVAL;
  135. if (usin->sin6_family != AF_INET6)
  136. return -EAFNOSUPPORT;
  137. if (np->sndflow)
  138. fl6_flowlabel = usin->sin6_flowinfo & IPV6_FLOWINFO_MASK;
  139. if (ipv6_addr_any(&usin->sin6_addr)) {
  140. /*
  141. * connect to self
  142. */
  143. if (ipv6_addr_v4mapped(&sk->sk_v6_rcv_saddr))
  144. ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK),
  145. &usin->sin6_addr);
  146. else
  147. usin->sin6_addr = in6addr_loopback;
  148. }
  149. addr_type = ipv6_addr_type(&usin->sin6_addr);
  150. daddr = &usin->sin6_addr;
  151. if (addr_type & IPV6_ADDR_MAPPED) {
  152. struct sockaddr_in sin;
  153. if (__ipv6_only_sock(sk)) {
  154. err = -ENETUNREACH;
  155. goto out;
  156. }
  157. sin.sin_family = AF_INET;
  158. sin.sin_addr.s_addr = daddr->s6_addr32[3];
  159. sin.sin_port = usin->sin6_port;
  160. err = __ip4_datagram_connect(sk,
  161. (struct sockaddr *) &sin,
  162. sizeof(sin));
  163. ipv4_connected:
  164. if (err)
  165. goto out;
  166. ipv6_addr_set_v4mapped(inet->inet_daddr, &sk->sk_v6_daddr);
  167. if (ipv6_addr_any(&np->saddr) ||
  168. ipv6_mapped_addr_any(&np->saddr))
  169. ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
  170. if (ipv6_addr_any(&sk->sk_v6_rcv_saddr) ||
  171. ipv6_mapped_addr_any(&sk->sk_v6_rcv_saddr)) {
  172. ipv6_addr_set_v4mapped(inet->inet_rcv_saddr,
  173. &sk->sk_v6_rcv_saddr);
  174. if (sk->sk_prot->rehash)
  175. sk->sk_prot->rehash(sk);
  176. }
  177. goto out;
  178. }
  179. if (__ipv6_addr_needs_scope_id(addr_type)) {
  180. if (addr_len >= sizeof(struct sockaddr_in6) &&
  181. usin->sin6_scope_id) {
  182. if (!sk_dev_equal_l3scope(sk, usin->sin6_scope_id)) {
  183. err = -EINVAL;
  184. goto out;
  185. }
  186. sk->sk_bound_dev_if = usin->sin6_scope_id;
  187. }
  188. if (!sk->sk_bound_dev_if && (addr_type & IPV6_ADDR_MULTICAST))
  189. sk->sk_bound_dev_if = np->mcast_oif;
  190. /* Connect to link-local address requires an interface */
  191. if (!sk->sk_bound_dev_if) {
  192. err = -EINVAL;
  193. goto out;
  194. }
  195. }
  196. /* save the current peer information before updating it */
  197. old_daddr = sk->sk_v6_daddr;
  198. old_fl6_flowlabel = np->flow_label;
  199. old_dport = inet->inet_dport;
  200. sk->sk_v6_daddr = *daddr;
  201. np->flow_label = fl6_flowlabel;
  202. inet->inet_dport = usin->sin6_port;
  203. /*
  204. * Check for a route to destination an obtain the
  205. * destination cache for it.
  206. */
  207. err = ip6_datagram_dst_update(sk, true);
  208. if (err) {
  209. /* Restore the socket peer info, to keep it consistent with
  210. * the old socket state
  211. */
  212. sk->sk_v6_daddr = old_daddr;
  213. np->flow_label = old_fl6_flowlabel;
  214. inet->inet_dport = old_dport;
  215. goto out;
  216. }
  217. reuseport_has_conns(sk, true);
  218. sk->sk_state = TCP_ESTABLISHED;
  219. sk_set_txhash(sk);
  220. out:
  221. return err;
  222. }
  223. EXPORT_SYMBOL_GPL(__ip6_datagram_connect);
  224. int ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
  225. {
  226. int res;
  227. lock_sock(sk);
  228. res = __ip6_datagram_connect(sk, uaddr, addr_len);
  229. release_sock(sk);
  230. return res;
  231. }
  232. EXPORT_SYMBOL_GPL(ip6_datagram_connect);
  233. int ip6_datagram_connect_v6_only(struct sock *sk, struct sockaddr *uaddr,
  234. int addr_len)
  235. {
  236. DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, uaddr);
  237. if (sin6->sin6_family != AF_INET6)
  238. return -EAFNOSUPPORT;
  239. return ip6_datagram_connect(sk, uaddr, addr_len);
  240. }
  241. EXPORT_SYMBOL_GPL(ip6_datagram_connect_v6_only);
  242. void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
  243. __be16 port, u32 info, u8 *payload)
  244. {
  245. struct ipv6_pinfo *np = inet6_sk(sk);
  246. struct icmp6hdr *icmph = icmp6_hdr(skb);
  247. struct sock_exterr_skb *serr;
  248. if (!np->recverr)
  249. return;
  250. skb = skb_clone(skb, GFP_ATOMIC);
  251. if (!skb)
  252. return;
  253. skb->protocol = htons(ETH_P_IPV6);
  254. serr = SKB_EXT_ERR(skb);
  255. serr->ee.ee_errno = err;
  256. serr->ee.ee_origin = SO_EE_ORIGIN_ICMP6;
  257. serr->ee.ee_type = icmph->icmp6_type;
  258. serr->ee.ee_code = icmph->icmp6_code;
  259. serr->ee.ee_pad = 0;
  260. serr->ee.ee_info = info;
  261. serr->ee.ee_data = 0;
  262. serr->addr_offset = (u8 *)&(((struct ipv6hdr *)(icmph + 1))->daddr) -
  263. skb_network_header(skb);
  264. serr->port = port;
  265. __skb_pull(skb, payload - skb->data);
  266. skb_reset_transport_header(skb);
  267. if (sock_queue_err_skb(sk, skb))
  268. kfree_skb(skb);
  269. }
  270. void ipv6_local_error(struct sock *sk, int err, struct flowi6 *fl6, u32 info)
  271. {
  272. const struct ipv6_pinfo *np = inet6_sk(sk);
  273. struct sock_exterr_skb *serr;
  274. struct ipv6hdr *iph;
  275. struct sk_buff *skb;
  276. if (!np->recverr)
  277. return;
  278. skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
  279. if (!skb)
  280. return;
  281. skb->protocol = htons(ETH_P_IPV6);
  282. skb_put(skb, sizeof(struct ipv6hdr));
  283. skb_reset_network_header(skb);
  284. iph = ipv6_hdr(skb);
  285. iph->daddr = fl6->daddr;
  286. ip6_flow_hdr(iph, 0, 0);
  287. serr = SKB_EXT_ERR(skb);
  288. serr->ee.ee_errno = err;
  289. serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
  290. serr->ee.ee_type = 0;
  291. serr->ee.ee_code = 0;
  292. serr->ee.ee_pad = 0;
  293. serr->ee.ee_info = info;
  294. serr->ee.ee_data = 0;
  295. serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
  296. serr->port = fl6->fl6_dport;
  297. __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
  298. skb_reset_transport_header(skb);
  299. if (sock_queue_err_skb(sk, skb))
  300. kfree_skb(skb);
  301. }
  302. void ipv6_local_rxpmtu(struct sock *sk, struct flowi6 *fl6, u32 mtu)
  303. {
  304. struct ipv6_pinfo *np = inet6_sk(sk);
  305. struct ipv6hdr *iph;
  306. struct sk_buff *skb;
  307. struct ip6_mtuinfo *mtu_info;
  308. if (!np->rxopt.bits.rxpmtu)
  309. return;
  310. skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
  311. if (!skb)
  312. return;
  313. skb_put(skb, sizeof(struct ipv6hdr));
  314. skb_reset_network_header(skb);
  315. iph = ipv6_hdr(skb);
  316. iph->daddr = fl6->daddr;
  317. mtu_info = IP6CBMTU(skb);
  318. mtu_info->ip6m_mtu = mtu;
  319. mtu_info->ip6m_addr.sin6_family = AF_INET6;
  320. mtu_info->ip6m_addr.sin6_port = 0;
  321. mtu_info->ip6m_addr.sin6_flowinfo = 0;
  322. mtu_info->ip6m_addr.sin6_scope_id = fl6->flowi6_oif;
  323. mtu_info->ip6m_addr.sin6_addr = ipv6_hdr(skb)->daddr;
  324. __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
  325. skb_reset_transport_header(skb);
  326. skb = xchg(&np->rxpmtu, skb);
  327. kfree_skb(skb);
  328. }
  329. /* For some errors we have valid addr_offset even with zero payload and
  330. * zero port. Also, addr_offset should be supported if port is set.
  331. */
  332. static inline bool ipv6_datagram_support_addr(struct sock_exterr_skb *serr)
  333. {
  334. return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6 ||
  335. serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
  336. serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port;
  337. }
  338. /* IPv6 supports cmsg on all origins aside from SO_EE_ORIGIN_LOCAL.
  339. *
  340. * At one point, excluding local errors was a quick test to identify icmp/icmp6
  341. * errors. This is no longer true, but the test remained, so the v6 stack,
  342. * unlike v4, also honors cmsg requests on all wifi and timestamp errors.
  343. */
  344. static bool ip6_datagram_support_cmsg(struct sk_buff *skb,
  345. struct sock_exterr_skb *serr)
  346. {
  347. if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
  348. serr->ee.ee_origin == SO_EE_ORIGIN_ICMP6)
  349. return true;
  350. if (serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL)
  351. return false;
  352. if (!IP6CB(skb)->iif)
  353. return false;
  354. return true;
  355. }
  356. /*
  357. * Handle MSG_ERRQUEUE
  358. */
  359. int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
  360. {
  361. struct ipv6_pinfo *np = inet6_sk(sk);
  362. struct sock_exterr_skb *serr;
  363. struct sk_buff *skb;
  364. DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name);
  365. struct {
  366. struct sock_extended_err ee;
  367. struct sockaddr_in6 offender;
  368. } errhdr;
  369. int err;
  370. int copied;
  371. err = -EAGAIN;
  372. skb = sock_dequeue_err_skb(sk);
  373. if (!skb)
  374. goto out;
  375. copied = skb->len;
  376. if (copied > len) {
  377. msg->msg_flags |= MSG_TRUNC;
  378. copied = len;
  379. }
  380. err = skb_copy_datagram_msg(skb, 0, msg, copied);
  381. if (unlikely(err)) {
  382. kfree_skb(skb);
  383. return err;
  384. }
  385. sock_recv_timestamp(msg, sk, skb);
  386. serr = SKB_EXT_ERR(skb);
  387. if (sin && ipv6_datagram_support_addr(serr)) {
  388. const unsigned char *nh = skb_network_header(skb);
  389. sin->sin6_family = AF_INET6;
  390. sin->sin6_flowinfo = 0;
  391. sin->sin6_port = serr->port;
  392. if (skb->protocol == htons(ETH_P_IPV6)) {
  393. const struct ipv6hdr *ip6h = container_of((struct in6_addr *)(nh + serr->addr_offset),
  394. struct ipv6hdr, daddr);
  395. sin->sin6_addr = ip6h->daddr;
  396. if (np->sndflow)
  397. sin->sin6_flowinfo = ip6_flowinfo(ip6h);
  398. sin->sin6_scope_id =
  399. ipv6_iface_scope_id(&sin->sin6_addr,
  400. IP6CB(skb)->iif);
  401. } else {
  402. ipv6_addr_set_v4mapped(*(__be32 *)(nh + serr->addr_offset),
  403. &sin->sin6_addr);
  404. sin->sin6_scope_id = 0;
  405. }
  406. *addr_len = sizeof(*sin);
  407. }
  408. memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
  409. sin = &errhdr.offender;
  410. memset(sin, 0, sizeof(*sin));
  411. if (ip6_datagram_support_cmsg(skb, serr)) {
  412. sin->sin6_family = AF_INET6;
  413. if (np->rxopt.all)
  414. ip6_datagram_recv_common_ctl(sk, msg, skb);
  415. if (skb->protocol == htons(ETH_P_IPV6)) {
  416. sin->sin6_addr = ipv6_hdr(skb)->saddr;
  417. if (np->rxopt.all)
  418. ip6_datagram_recv_specific_ctl(sk, msg, skb);
  419. sin->sin6_scope_id =
  420. ipv6_iface_scope_id(&sin->sin6_addr,
  421. IP6CB(skb)->iif);
  422. } else {
  423. ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
  424. &sin->sin6_addr);
  425. if (inet_sk(sk)->cmsg_flags)
  426. ip_cmsg_recv(msg, skb);
  427. }
  428. }
  429. put_cmsg(msg, SOL_IPV6, IPV6_RECVERR, sizeof(errhdr), &errhdr);
  430. /* Now we could try to dump offended packet options */
  431. msg->msg_flags |= MSG_ERRQUEUE;
  432. err = copied;
  433. consume_skb(skb);
  434. out:
  435. return err;
  436. }
  437. EXPORT_SYMBOL_GPL(ipv6_recv_error);
  438. /*
  439. * Handle IPV6_RECVPATHMTU
  440. */
  441. int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len,
  442. int *addr_len)
  443. {
  444. struct ipv6_pinfo *np = inet6_sk(sk);
  445. struct sk_buff *skb;
  446. struct ip6_mtuinfo mtu_info;
  447. DECLARE_SOCKADDR(struct sockaddr_in6 *, sin, msg->msg_name);
  448. int err;
  449. int copied;
  450. err = -EAGAIN;
  451. skb = xchg(&np->rxpmtu, NULL);
  452. if (!skb)
  453. goto out;
  454. copied = skb->len;
  455. if (copied > len) {
  456. msg->msg_flags |= MSG_TRUNC;
  457. copied = len;
  458. }
  459. err = skb_copy_datagram_msg(skb, 0, msg, copied);
  460. if (err)
  461. goto out_free_skb;
  462. sock_recv_timestamp(msg, sk, skb);
  463. memcpy(&mtu_info, IP6CBMTU(skb), sizeof(mtu_info));
  464. if (sin) {
  465. sin->sin6_family = AF_INET6;
  466. sin->sin6_flowinfo = 0;
  467. sin->sin6_port = 0;
  468. sin->sin6_scope_id = mtu_info.ip6m_addr.sin6_scope_id;
  469. sin->sin6_addr = mtu_info.ip6m_addr.sin6_addr;
  470. *addr_len = sizeof(*sin);
  471. }
  472. put_cmsg(msg, SOL_IPV6, IPV6_PATHMTU, sizeof(mtu_info), &mtu_info);
  473. err = copied;
  474. out_free_skb:
  475. kfree_skb(skb);
  476. out:
  477. return err;
  478. }
  479. void ip6_datagram_recv_common_ctl(struct sock *sk, struct msghdr *msg,
  480. struct sk_buff *skb)
  481. {
  482. struct ipv6_pinfo *np = inet6_sk(sk);
  483. bool is_ipv6 = skb->protocol == htons(ETH_P_IPV6);
  484. if (np->rxopt.bits.rxinfo) {
  485. struct in6_pktinfo src_info;
  486. if (is_ipv6) {
  487. src_info.ipi6_ifindex = IP6CB(skb)->iif;
  488. src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
  489. } else {
  490. src_info.ipi6_ifindex =
  491. PKTINFO_SKB_CB(skb)->ipi_ifindex;
  492. ipv6_addr_set_v4mapped(ip_hdr(skb)->daddr,
  493. &src_info.ipi6_addr);
  494. }
  495. if (src_info.ipi6_ifindex >= 0)
  496. put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO,
  497. sizeof(src_info), &src_info);
  498. }
  499. }
  500. void ip6_datagram_recv_specific_ctl(struct sock *sk, struct msghdr *msg,
  501. struct sk_buff *skb)
  502. {
  503. struct ipv6_pinfo *np = inet6_sk(sk);
  504. struct inet6_skb_parm *opt = IP6CB(skb);
  505. unsigned char *nh = skb_network_header(skb);
  506. if (np->rxopt.bits.rxhlim) {
  507. int hlim = ipv6_hdr(skb)->hop_limit;
  508. put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
  509. }
  510. if (np->rxopt.bits.rxtclass) {
  511. int tclass = ipv6_get_dsfield(ipv6_hdr(skb));
  512. put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
  513. }
  514. if (np->rxopt.bits.rxflow) {
  515. __be32 flowinfo = ip6_flowinfo((struct ipv6hdr *)nh);
  516. if (flowinfo)
  517. put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
  518. }
  519. /* HbH is allowed only once */
  520. if (np->rxopt.bits.hopopts && (opt->flags & IP6SKB_HOPBYHOP)) {
  521. u8 *ptr = nh + sizeof(struct ipv6hdr);
  522. put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr);
  523. }
  524. if (opt->lastopt &&
  525. (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) {
  526. /*
  527. * Silly enough, but we need to reparse in order to
  528. * report extension headers (except for HbH)
  529. * in order.
  530. *
  531. * Also note that IPV6_RECVRTHDRDSTOPTS is NOT
  532. * (and WILL NOT be) defined because
  533. * IPV6_RECVDSTOPTS is more generic. --yoshfuji
  534. */
  535. unsigned int off = sizeof(struct ipv6hdr);
  536. u8 nexthdr = ipv6_hdr(skb)->nexthdr;
  537. while (off <= opt->lastopt) {
  538. unsigned int len;
  539. u8 *ptr = nh + off;
  540. switch (nexthdr) {
  541. case IPPROTO_DSTOPTS:
  542. nexthdr = ptr[0];
  543. len = (ptr[1] + 1) << 3;
  544. if (np->rxopt.bits.dstopts)
  545. put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr);
  546. break;
  547. case IPPROTO_ROUTING:
  548. nexthdr = ptr[0];
  549. len = (ptr[1] + 1) << 3;
  550. if (np->rxopt.bits.srcrt)
  551. put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr);
  552. break;
  553. case IPPROTO_AH:
  554. nexthdr = ptr[0];
  555. len = (ptr[1] + 2) << 2;
  556. break;
  557. default:
  558. nexthdr = ptr[0];
  559. len = (ptr[1] + 1) << 3;
  560. break;
  561. }
  562. off += len;
  563. }
  564. }
  565. /* socket options in old style */
  566. if (np->rxopt.bits.rxoinfo) {
  567. struct in6_pktinfo src_info;
  568. src_info.ipi6_ifindex = opt->iif;
  569. src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
  570. put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
  571. }
  572. if (np->rxopt.bits.rxohlim) {
  573. int hlim = ipv6_hdr(skb)->hop_limit;
  574. put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
  575. }
  576. if (np->rxopt.bits.ohopopts && (opt->flags & IP6SKB_HOPBYHOP)) {
  577. u8 *ptr = nh + sizeof(struct ipv6hdr);
  578. put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr);
  579. }
  580. if (np->rxopt.bits.odstopts && opt->dst0) {
  581. u8 *ptr = nh + opt->dst0;
  582. put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
  583. }
  584. if (np->rxopt.bits.osrcrt && opt->srcrt) {
  585. struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt);
  586. put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr);
  587. }
  588. if (np->rxopt.bits.odstopts && opt->dst1) {
  589. u8 *ptr = nh + opt->dst1;
  590. put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
  591. }
  592. if (np->rxopt.bits.rxorigdstaddr) {
  593. struct sockaddr_in6 sin6;
  594. __be16 _ports[2], *ports;
  595. ports = skb_header_pointer(skb, skb_transport_offset(skb),
  596. sizeof(_ports), &_ports);
  597. if (ports) {
  598. /* All current transport protocols have the port numbers in the
  599. * first four bytes of the transport header and this function is
  600. * written with this assumption in mind.
  601. */
  602. sin6.sin6_family = AF_INET6;
  603. sin6.sin6_addr = ipv6_hdr(skb)->daddr;
  604. sin6.sin6_port = ports[1];
  605. sin6.sin6_flowinfo = 0;
  606. sin6.sin6_scope_id =
  607. ipv6_iface_scope_id(&ipv6_hdr(skb)->daddr,
  608. opt->iif);
  609. put_cmsg(msg, SOL_IPV6, IPV6_ORIGDSTADDR, sizeof(sin6), &sin6);
  610. }
  611. }
  612. if (np->rxopt.bits.recvfragsize && opt->frag_max_size) {
  613. int val = opt->frag_max_size;
  614. put_cmsg(msg, SOL_IPV6, IPV6_RECVFRAGSIZE, sizeof(val), &val);
  615. }
  616. }
  617. void ip6_datagram_recv_ctl(struct sock *sk, struct msghdr *msg,
  618. struct sk_buff *skb)
  619. {
  620. ip6_datagram_recv_common_ctl(sk, msg, skb);
  621. ip6_datagram_recv_specific_ctl(sk, msg, skb);
  622. }
  623. EXPORT_SYMBOL_GPL(ip6_datagram_recv_ctl);
  624. int ip6_datagram_send_ctl(struct net *net, struct sock *sk,
  625. struct msghdr *msg, struct flowi6 *fl6,
  626. struct ipcm6_cookie *ipc6)
  627. {
  628. struct in6_pktinfo *src_info;
  629. struct cmsghdr *cmsg;
  630. struct ipv6_rt_hdr *rthdr;
  631. struct ipv6_opt_hdr *hdr;
  632. struct ipv6_txoptions *opt = ipc6->opt;
  633. int len;
  634. int err = 0;
  635. for_each_cmsghdr(cmsg, msg) {
  636. int addr_type;
  637. if (!CMSG_OK(msg, cmsg)) {
  638. err = -EINVAL;
  639. goto exit_f;
  640. }
  641. if (cmsg->cmsg_level == SOL_SOCKET) {
  642. err = __sock_cmsg_send(sk, msg, cmsg, &ipc6->sockc);
  643. if (err)
  644. return err;
  645. continue;
  646. }
  647. if (cmsg->cmsg_level != SOL_IPV6)
  648. continue;
  649. switch (cmsg->cmsg_type) {
  650. case IPV6_PKTINFO:
  651. case IPV6_2292PKTINFO:
  652. {
  653. struct net_device *dev = NULL;
  654. if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) {
  655. err = -EINVAL;
  656. goto exit_f;
  657. }
  658. src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
  659. if (src_info->ipi6_ifindex) {
  660. if (fl6->flowi6_oif &&
  661. src_info->ipi6_ifindex != fl6->flowi6_oif)
  662. return -EINVAL;
  663. fl6->flowi6_oif = src_info->ipi6_ifindex;
  664. }
  665. addr_type = __ipv6_addr_type(&src_info->ipi6_addr);
  666. rcu_read_lock();
  667. if (fl6->flowi6_oif) {
  668. dev = dev_get_by_index_rcu(net, fl6->flowi6_oif);
  669. if (!dev) {
  670. rcu_read_unlock();
  671. return -ENODEV;
  672. }
  673. } else if (addr_type & IPV6_ADDR_LINKLOCAL) {
  674. rcu_read_unlock();
  675. return -EINVAL;
  676. }
  677. if (addr_type != IPV6_ADDR_ANY) {
  678. int strict = __ipv6_addr_src_scope(addr_type) <= IPV6_ADDR_SCOPE_LINKLOCAL;
  679. if (!ipv6_can_nonlocal_bind(net, inet_sk(sk)) &&
  680. !ipv6_chk_addr_and_flags(net, &src_info->ipi6_addr,
  681. dev, !strict, 0,
  682. IFA_F_TENTATIVE) &&
  683. !ipv6_chk_acast_addr_src(net, dev,
  684. &src_info->ipi6_addr))
  685. err = -EINVAL;
  686. else
  687. fl6->saddr = src_info->ipi6_addr;
  688. }
  689. rcu_read_unlock();
  690. if (err)
  691. goto exit_f;
  692. break;
  693. }
  694. case IPV6_FLOWINFO:
  695. if (cmsg->cmsg_len < CMSG_LEN(4)) {
  696. err = -EINVAL;
  697. goto exit_f;
  698. }
  699. if (fl6->flowlabel&IPV6_FLOWINFO_MASK) {
  700. if ((fl6->flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) {
  701. err = -EINVAL;
  702. goto exit_f;
  703. }
  704. }
  705. fl6->flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg);
  706. break;
  707. case IPV6_2292HOPOPTS:
  708. case IPV6_HOPOPTS:
  709. if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
  710. err = -EINVAL;
  711. goto exit_f;
  712. }
  713. hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
  714. len = ((hdr->hdrlen + 1) << 3);
  715. if (cmsg->cmsg_len < CMSG_LEN(len)) {
  716. err = -EINVAL;
  717. goto exit_f;
  718. }
  719. if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
  720. err = -EPERM;
  721. goto exit_f;
  722. }
  723. opt->opt_nflen += len;
  724. opt->hopopt = hdr;
  725. break;
  726. case IPV6_2292DSTOPTS:
  727. if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
  728. err = -EINVAL;
  729. goto exit_f;
  730. }
  731. hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
  732. len = ((hdr->hdrlen + 1) << 3);
  733. if (cmsg->cmsg_len < CMSG_LEN(len)) {
  734. err = -EINVAL;
  735. goto exit_f;
  736. }
  737. if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
  738. err = -EPERM;
  739. goto exit_f;
  740. }
  741. if (opt->dst1opt) {
  742. err = -EINVAL;
  743. goto exit_f;
  744. }
  745. opt->opt_flen += len;
  746. opt->dst1opt = hdr;
  747. break;
  748. case IPV6_DSTOPTS:
  749. case IPV6_RTHDRDSTOPTS:
  750. if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
  751. err = -EINVAL;
  752. goto exit_f;
  753. }
  754. hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
  755. len = ((hdr->hdrlen + 1) << 3);
  756. if (cmsg->cmsg_len < CMSG_LEN(len)) {
  757. err = -EINVAL;
  758. goto exit_f;
  759. }
  760. if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
  761. err = -EPERM;
  762. goto exit_f;
  763. }
  764. if (cmsg->cmsg_type == IPV6_DSTOPTS) {
  765. opt->opt_flen += len;
  766. opt->dst1opt = hdr;
  767. } else {
  768. opt->opt_nflen += len;
  769. opt->dst0opt = hdr;
  770. }
  771. break;
  772. case IPV6_2292RTHDR:
  773. case IPV6_RTHDR:
  774. if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) {
  775. err = -EINVAL;
  776. goto exit_f;
  777. }
  778. rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg);
  779. switch (rthdr->type) {
  780. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  781. case IPV6_SRCRT_TYPE_2:
  782. if (rthdr->hdrlen != 2 ||
  783. rthdr->segments_left != 1) {
  784. err = -EINVAL;
  785. goto exit_f;
  786. }
  787. break;
  788. #endif
  789. default:
  790. err = -EINVAL;
  791. goto exit_f;
  792. }
  793. len = ((rthdr->hdrlen + 1) << 3);
  794. if (cmsg->cmsg_len < CMSG_LEN(len)) {
  795. err = -EINVAL;
  796. goto exit_f;
  797. }
  798. /* segments left must also match */
  799. if ((rthdr->hdrlen >> 1) != rthdr->segments_left) {
  800. err = -EINVAL;
  801. goto exit_f;
  802. }
  803. opt->opt_nflen += len;
  804. opt->srcrt = rthdr;
  805. if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) {
  806. int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3);
  807. opt->opt_nflen += dsthdrlen;
  808. opt->dst0opt = opt->dst1opt;
  809. opt->dst1opt = NULL;
  810. opt->opt_flen -= dsthdrlen;
  811. }
  812. break;
  813. case IPV6_2292HOPLIMIT:
  814. case IPV6_HOPLIMIT:
  815. if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
  816. err = -EINVAL;
  817. goto exit_f;
  818. }
  819. ipc6->hlimit = *(int *)CMSG_DATA(cmsg);
  820. if (ipc6->hlimit < -1 || ipc6->hlimit > 0xff) {
  821. err = -EINVAL;
  822. goto exit_f;
  823. }
  824. break;
  825. case IPV6_TCLASS:
  826. {
  827. int tc;
  828. err = -EINVAL;
  829. if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
  830. goto exit_f;
  831. tc = *(int *)CMSG_DATA(cmsg);
  832. if (tc < -1 || tc > 0xff)
  833. goto exit_f;
  834. err = 0;
  835. ipc6->tclass = tc;
  836. break;
  837. }
  838. case IPV6_DONTFRAG:
  839. {
  840. int df;
  841. err = -EINVAL;
  842. if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
  843. goto exit_f;
  844. df = *(int *)CMSG_DATA(cmsg);
  845. if (df < 0 || df > 1)
  846. goto exit_f;
  847. err = 0;
  848. ipc6->dontfrag = df;
  849. break;
  850. }
  851. default:
  852. net_dbg_ratelimited("invalid cmsg type: %d\n",
  853. cmsg->cmsg_type);
  854. err = -EINVAL;
  855. goto exit_f;
  856. }
  857. }
  858. exit_f:
  859. return err;
  860. }
  861. EXPORT_SYMBOL_GPL(ip6_datagram_send_ctl);
  862. void __ip6_dgram_sock_seq_show(struct seq_file *seq, struct sock *sp,
  863. __u16 srcp, __u16 destp, int rqueue, int bucket)
  864. {
  865. const struct in6_addr *dest, *src;
  866. dest = &sp->sk_v6_daddr;
  867. src = &sp->sk_v6_rcv_saddr;
  868. seq_printf(seq,
  869. "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
  870. "%02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d\n",
  871. bucket,
  872. src->s6_addr32[0], src->s6_addr32[1],
  873. src->s6_addr32[2], src->s6_addr32[3], srcp,
  874. dest->s6_addr32[0], dest->s6_addr32[1],
  875. dest->s6_addr32[2], dest->s6_addr32[3], destp,
  876. sp->sk_state,
  877. sk_wmem_alloc_get(sp),
  878. rqueue,
  879. 0, 0L, 0,
  880. from_kuid_munged(seq_user_ns(seq), sock_i_uid(sp)),
  881. 0,
  882. sock_i_ino(sp),
  883. refcount_read(&sp->sk_refcnt), sp,
  884. atomic_read(&sp->sk_drops));
  885. }