datagram.c 23 KB

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