datagram.c 23 KB

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