datagram.c 23 KB

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