datagram.c 24 KB

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