datagram.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  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 <linux/errqueue.h>
  32. #include <asm/uaccess.h>
  33. static bool ipv6_mapped_addr_any(const struct in6_addr *a)
  34. {
  35. return ipv6_addr_v4mapped(a) && (a->s6_addr32[3] == 0);
  36. }
  37. int ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
  38. {
  39. struct sockaddr_in6 *usin = (struct sockaddr_in6 *) uaddr;
  40. struct inet_sock *inet = inet_sk(sk);
  41. struct ipv6_pinfo *np = inet6_sk(sk);
  42. struct in6_addr *daddr, *final_p, final;
  43. struct dst_entry *dst;
  44. struct flowi6 fl6;
  45. struct ip6_flowlabel *flowlabel = NULL;
  46. struct ipv6_txoptions *opt;
  47. int addr_type;
  48. int err;
  49. if (usin->sin6_family == AF_INET) {
  50. if (__ipv6_only_sock(sk))
  51. return -EAFNOSUPPORT;
  52. err = ip4_datagram_connect(sk, uaddr, addr_len);
  53. goto ipv4_connected;
  54. }
  55. if (addr_len < SIN6_LEN_RFC2133)
  56. return -EINVAL;
  57. if (usin->sin6_family != AF_INET6)
  58. return -EAFNOSUPPORT;
  59. memset(&fl6, 0, sizeof(fl6));
  60. if (np->sndflow) {
  61. fl6.flowlabel = usin->sin6_flowinfo&IPV6_FLOWINFO_MASK;
  62. if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
  63. flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
  64. if (flowlabel == NULL)
  65. return -EINVAL;
  66. usin->sin6_addr = flowlabel->dst;
  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, &np->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(&np->rcv_saddr) ||
  97. ipv6_mapped_addr_any(&np->rcv_saddr)) {
  98. ipv6_addr_set_v4mapped(inet->inet_rcv_saddr,
  99. &np->rcv_saddr);
  100. if (sk->sk_prot->rehash)
  101. sk->sk_prot->rehash(sk);
  102. }
  103. goto out;
  104. }
  105. if (addr_type&IPV6_ADDR_LINKLOCAL) {
  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. np->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 = np->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, true);
  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(&np->rcv_saddr)) {
  152. np->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, &np->daddr) ?
  159. &np->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. out:
  167. fl6_sock_release(flowlabel);
  168. return err;
  169. }
  170. EXPORT_SYMBOL_GPL(ip6_datagram_connect);
  171. void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
  172. __be16 port, u32 info, u8 *payload)
  173. {
  174. struct ipv6_pinfo *np = inet6_sk(sk);
  175. struct icmp6hdr *icmph = icmp6_hdr(skb);
  176. struct sock_exterr_skb *serr;
  177. if (!np->recverr)
  178. return;
  179. skb = skb_clone(skb, GFP_ATOMIC);
  180. if (!skb)
  181. return;
  182. skb->protocol = htons(ETH_P_IPV6);
  183. serr = SKB_EXT_ERR(skb);
  184. serr->ee.ee_errno = err;
  185. serr->ee.ee_origin = SO_EE_ORIGIN_ICMP6;
  186. serr->ee.ee_type = icmph->icmp6_type;
  187. serr->ee.ee_code = icmph->icmp6_code;
  188. serr->ee.ee_pad = 0;
  189. serr->ee.ee_info = info;
  190. serr->ee.ee_data = 0;
  191. serr->addr_offset = (u8 *)&(((struct ipv6hdr *)(icmph + 1))->daddr) -
  192. skb_network_header(skb);
  193. serr->port = port;
  194. __skb_pull(skb, payload - skb->data);
  195. skb_reset_transport_header(skb);
  196. if (sock_queue_err_skb(sk, skb))
  197. kfree_skb(skb);
  198. }
  199. void ipv6_local_error(struct sock *sk, int err, struct flowi6 *fl6, u32 info)
  200. {
  201. struct ipv6_pinfo *np = inet6_sk(sk);
  202. struct sock_exterr_skb *serr;
  203. struct ipv6hdr *iph;
  204. struct sk_buff *skb;
  205. if (!np->recverr)
  206. return;
  207. skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
  208. if (!skb)
  209. return;
  210. skb->protocol = htons(ETH_P_IPV6);
  211. skb_put(skb, sizeof(struct ipv6hdr));
  212. skb_reset_network_header(skb);
  213. iph = ipv6_hdr(skb);
  214. iph->daddr = fl6->daddr;
  215. serr = SKB_EXT_ERR(skb);
  216. serr->ee.ee_errno = err;
  217. serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
  218. serr->ee.ee_type = 0;
  219. serr->ee.ee_code = 0;
  220. serr->ee.ee_pad = 0;
  221. serr->ee.ee_info = info;
  222. serr->ee.ee_data = 0;
  223. serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
  224. serr->port = fl6->fl6_dport;
  225. __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
  226. skb_reset_transport_header(skb);
  227. if (sock_queue_err_skb(sk, skb))
  228. kfree_skb(skb);
  229. }
  230. void ipv6_local_rxpmtu(struct sock *sk, struct flowi6 *fl6, u32 mtu)
  231. {
  232. struct ipv6_pinfo *np = inet6_sk(sk);
  233. struct ipv6hdr *iph;
  234. struct sk_buff *skb;
  235. struct ip6_mtuinfo *mtu_info;
  236. if (!np->rxopt.bits.rxpmtu)
  237. return;
  238. skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
  239. if (!skb)
  240. return;
  241. skb_put(skb, sizeof(struct ipv6hdr));
  242. skb_reset_network_header(skb);
  243. iph = ipv6_hdr(skb);
  244. iph->daddr = fl6->daddr;
  245. mtu_info = IP6CBMTU(skb);
  246. mtu_info->ip6m_mtu = mtu;
  247. mtu_info->ip6m_addr.sin6_family = AF_INET6;
  248. mtu_info->ip6m_addr.sin6_port = 0;
  249. mtu_info->ip6m_addr.sin6_flowinfo = 0;
  250. mtu_info->ip6m_addr.sin6_scope_id = fl6->flowi6_oif;
  251. mtu_info->ip6m_addr.sin6_addr = ipv6_hdr(skb)->daddr;
  252. __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
  253. skb_reset_transport_header(skb);
  254. skb = xchg(&np->rxpmtu, skb);
  255. kfree_skb(skb);
  256. }
  257. /*
  258. * Handle MSG_ERRQUEUE
  259. */
  260. int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len)
  261. {
  262. struct ipv6_pinfo *np = inet6_sk(sk);
  263. struct sock_exterr_skb *serr;
  264. struct sk_buff *skb, *skb2;
  265. struct sockaddr_in6 *sin;
  266. struct {
  267. struct sock_extended_err ee;
  268. struct sockaddr_in6 offender;
  269. } errhdr;
  270. int err;
  271. int copied;
  272. err = -EAGAIN;
  273. skb = skb_dequeue(&sk->sk_error_queue);
  274. if (skb == NULL)
  275. goto out;
  276. copied = skb->len;
  277. if (copied > len) {
  278. msg->msg_flags |= MSG_TRUNC;
  279. copied = len;
  280. }
  281. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  282. if (err)
  283. goto out_free_skb;
  284. sock_recv_timestamp(msg, sk, skb);
  285. serr = SKB_EXT_ERR(skb);
  286. sin = (struct sockaddr_in6 *)msg->msg_name;
  287. if (sin) {
  288. const unsigned char *nh = skb_network_header(skb);
  289. sin->sin6_family = AF_INET6;
  290. sin->sin6_flowinfo = 0;
  291. sin->sin6_port = serr->port;
  292. sin->sin6_scope_id = 0;
  293. if (skb->protocol == htons(ETH_P_IPV6)) {
  294. sin->sin6_addr =
  295. *(struct in6_addr *)(nh + serr->addr_offset);
  296. if (np->sndflow)
  297. sin->sin6_flowinfo =
  298. (*(__be32 *)(nh + serr->addr_offset - 24) &
  299. IPV6_FLOWINFO_MASK);
  300. if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
  301. sin->sin6_scope_id = IP6CB(skb)->iif;
  302. } else {
  303. ipv6_addr_set_v4mapped(*(__be32 *)(nh + serr->addr_offset),
  304. &sin->sin6_addr);
  305. }
  306. }
  307. memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
  308. sin = &errhdr.offender;
  309. sin->sin6_family = AF_UNSPEC;
  310. if (serr->ee.ee_origin != SO_EE_ORIGIN_LOCAL) {
  311. sin->sin6_family = AF_INET6;
  312. sin->sin6_flowinfo = 0;
  313. sin->sin6_scope_id = 0;
  314. if (skb->protocol == htons(ETH_P_IPV6)) {
  315. sin->sin6_addr = ipv6_hdr(skb)->saddr;
  316. if (np->rxopt.all)
  317. ip6_datagram_recv_ctl(sk, msg, skb);
  318. if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
  319. sin->sin6_scope_id = IP6CB(skb)->iif;
  320. } else {
  321. struct inet_sock *inet = inet_sk(sk);
  322. ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
  323. &sin->sin6_addr);
  324. if (inet->cmsg_flags)
  325. ip_cmsg_recv(msg, skb);
  326. }
  327. }
  328. put_cmsg(msg, SOL_IPV6, IPV6_RECVERR, sizeof(errhdr), &errhdr);
  329. /* Now we could try to dump offended packet options */
  330. msg->msg_flags |= MSG_ERRQUEUE;
  331. err = copied;
  332. /* Reset and regenerate socket error */
  333. spin_lock_bh(&sk->sk_error_queue.lock);
  334. sk->sk_err = 0;
  335. if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
  336. sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
  337. spin_unlock_bh(&sk->sk_error_queue.lock);
  338. sk->sk_error_report(sk);
  339. } else {
  340. spin_unlock_bh(&sk->sk_error_queue.lock);
  341. }
  342. out_free_skb:
  343. kfree_skb(skb);
  344. out:
  345. return err;
  346. }
  347. EXPORT_SYMBOL_GPL(ipv6_recv_error);
  348. /*
  349. * Handle IPV6_RECVPATHMTU
  350. */
  351. int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len)
  352. {
  353. struct ipv6_pinfo *np = inet6_sk(sk);
  354. struct sk_buff *skb;
  355. struct sockaddr_in6 *sin;
  356. struct ip6_mtuinfo mtu_info;
  357. int err;
  358. int copied;
  359. err = -EAGAIN;
  360. skb = xchg(&np->rxpmtu, NULL);
  361. if (skb == NULL)
  362. goto out;
  363. copied = skb->len;
  364. if (copied > len) {
  365. msg->msg_flags |= MSG_TRUNC;
  366. copied = len;
  367. }
  368. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  369. if (err)
  370. goto out_free_skb;
  371. sock_recv_timestamp(msg, sk, skb);
  372. memcpy(&mtu_info, IP6CBMTU(skb), sizeof(mtu_info));
  373. sin = (struct sockaddr_in6 *)msg->msg_name;
  374. if (sin) {
  375. sin->sin6_family = AF_INET6;
  376. sin->sin6_flowinfo = 0;
  377. sin->sin6_port = 0;
  378. sin->sin6_scope_id = mtu_info.ip6m_addr.sin6_scope_id;
  379. sin->sin6_addr = mtu_info.ip6m_addr.sin6_addr;
  380. }
  381. put_cmsg(msg, SOL_IPV6, IPV6_PATHMTU, sizeof(mtu_info), &mtu_info);
  382. err = copied;
  383. out_free_skb:
  384. kfree_skb(skb);
  385. out:
  386. return err;
  387. }
  388. int ip6_datagram_recv_ctl(struct sock *sk, struct msghdr *msg,
  389. struct sk_buff *skb)
  390. {
  391. struct ipv6_pinfo *np = inet6_sk(sk);
  392. struct inet6_skb_parm *opt = IP6CB(skb);
  393. unsigned char *nh = skb_network_header(skb);
  394. if (np->rxopt.bits.rxinfo) {
  395. struct in6_pktinfo src_info;
  396. src_info.ipi6_ifindex = opt->iif;
  397. src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
  398. put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
  399. }
  400. if (np->rxopt.bits.rxhlim) {
  401. int hlim = ipv6_hdr(skb)->hop_limit;
  402. put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
  403. }
  404. if (np->rxopt.bits.rxtclass) {
  405. int tclass = ipv6_tclass(ipv6_hdr(skb));
  406. put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
  407. }
  408. if (np->rxopt.bits.rxflow && (*(__be32 *)nh & IPV6_FLOWINFO_MASK)) {
  409. __be32 flowinfo = *(__be32 *)nh & IPV6_FLOWINFO_MASK;
  410. put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
  411. }
  412. /* HbH is allowed only once */
  413. if (np->rxopt.bits.hopopts && opt->hop) {
  414. u8 *ptr = nh + opt->hop;
  415. put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr);
  416. }
  417. if (opt->lastopt &&
  418. (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) {
  419. /*
  420. * Silly enough, but we need to reparse in order to
  421. * report extension headers (except for HbH)
  422. * in order.
  423. *
  424. * Also note that IPV6_RECVRTHDRDSTOPTS is NOT
  425. * (and WILL NOT be) defined because
  426. * IPV6_RECVDSTOPTS is more generic. --yoshfuji
  427. */
  428. unsigned int off = sizeof(struct ipv6hdr);
  429. u8 nexthdr = ipv6_hdr(skb)->nexthdr;
  430. while (off <= opt->lastopt) {
  431. unsigned int len;
  432. u8 *ptr = nh + off;
  433. switch (nexthdr) {
  434. case IPPROTO_DSTOPTS:
  435. nexthdr = ptr[0];
  436. len = (ptr[1] + 1) << 3;
  437. if (np->rxopt.bits.dstopts)
  438. put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr);
  439. break;
  440. case IPPROTO_ROUTING:
  441. nexthdr = ptr[0];
  442. len = (ptr[1] + 1) << 3;
  443. if (np->rxopt.bits.srcrt)
  444. put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr);
  445. break;
  446. case IPPROTO_AH:
  447. nexthdr = ptr[0];
  448. len = (ptr[1] + 2) << 2;
  449. break;
  450. default:
  451. nexthdr = ptr[0];
  452. len = (ptr[1] + 1) << 3;
  453. break;
  454. }
  455. off += len;
  456. }
  457. }
  458. /* socket options in old style */
  459. if (np->rxopt.bits.rxoinfo) {
  460. struct in6_pktinfo src_info;
  461. src_info.ipi6_ifindex = opt->iif;
  462. src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
  463. put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
  464. }
  465. if (np->rxopt.bits.rxohlim) {
  466. int hlim = ipv6_hdr(skb)->hop_limit;
  467. put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
  468. }
  469. if (np->rxopt.bits.ohopopts && opt->hop) {
  470. u8 *ptr = nh + opt->hop;
  471. put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr);
  472. }
  473. if (np->rxopt.bits.odstopts && opt->dst0) {
  474. u8 *ptr = nh + opt->dst0;
  475. put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
  476. }
  477. if (np->rxopt.bits.osrcrt && opt->srcrt) {
  478. struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt);
  479. put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr);
  480. }
  481. if (np->rxopt.bits.odstopts && opt->dst1) {
  482. u8 *ptr = nh + opt->dst1;
  483. put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
  484. }
  485. if (np->rxopt.bits.rxorigdstaddr) {
  486. struct sockaddr_in6 sin6;
  487. __be16 *ports = (__be16 *) skb_transport_header(skb);
  488. if (skb_transport_offset(skb) + 4 <= skb->len) {
  489. /* All current transport protocols have the port numbers in the
  490. * first four bytes of the transport header and this function is
  491. * written with this assumption in mind.
  492. */
  493. sin6.sin6_family = AF_INET6;
  494. sin6.sin6_addr = ipv6_hdr(skb)->daddr;
  495. sin6.sin6_port = ports[1];
  496. sin6.sin6_flowinfo = 0;
  497. sin6.sin6_scope_id = 0;
  498. put_cmsg(msg, SOL_IPV6, IPV6_ORIGDSTADDR, sizeof(sin6), &sin6);
  499. }
  500. }
  501. return 0;
  502. }
  503. EXPORT_SYMBOL_GPL(ip6_datagram_recv_ctl);
  504. int ip6_datagram_send_ctl(struct net *net, struct sock *sk,
  505. struct msghdr *msg, struct flowi6 *fl6,
  506. struct ipv6_txoptions *opt,
  507. int *hlimit, int *tclass, int *dontfrag)
  508. {
  509. struct in6_pktinfo *src_info;
  510. struct cmsghdr *cmsg;
  511. struct ipv6_rt_hdr *rthdr;
  512. struct ipv6_opt_hdr *hdr;
  513. int len;
  514. int err = 0;
  515. for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
  516. int addr_type;
  517. if (!CMSG_OK(msg, cmsg)) {
  518. err = -EINVAL;
  519. goto exit_f;
  520. }
  521. if (cmsg->cmsg_level != SOL_IPV6)
  522. continue;
  523. switch (cmsg->cmsg_type) {
  524. case IPV6_PKTINFO:
  525. case IPV6_2292PKTINFO:
  526. {
  527. struct net_device *dev = NULL;
  528. if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) {
  529. err = -EINVAL;
  530. goto exit_f;
  531. }
  532. src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
  533. if (src_info->ipi6_ifindex) {
  534. if (fl6->flowi6_oif &&
  535. src_info->ipi6_ifindex != fl6->flowi6_oif)
  536. return -EINVAL;
  537. fl6->flowi6_oif = src_info->ipi6_ifindex;
  538. }
  539. addr_type = __ipv6_addr_type(&src_info->ipi6_addr);
  540. rcu_read_lock();
  541. if (fl6->flowi6_oif) {
  542. dev = dev_get_by_index_rcu(net, fl6->flowi6_oif);
  543. if (!dev) {
  544. rcu_read_unlock();
  545. return -ENODEV;
  546. }
  547. } else if (addr_type & IPV6_ADDR_LINKLOCAL) {
  548. rcu_read_unlock();
  549. return -EINVAL;
  550. }
  551. if (addr_type != IPV6_ADDR_ANY) {
  552. int strict = __ipv6_addr_src_scope(addr_type) <= IPV6_ADDR_SCOPE_LINKLOCAL;
  553. if (!(inet_sk(sk)->freebind || inet_sk(sk)->transparent) &&
  554. !ipv6_chk_addr(net, &src_info->ipi6_addr,
  555. strict ? dev : NULL, 0))
  556. err = -EINVAL;
  557. else
  558. fl6->saddr = src_info->ipi6_addr;
  559. }
  560. rcu_read_unlock();
  561. if (err)
  562. goto exit_f;
  563. break;
  564. }
  565. case IPV6_FLOWINFO:
  566. if (cmsg->cmsg_len < CMSG_LEN(4)) {
  567. err = -EINVAL;
  568. goto exit_f;
  569. }
  570. if (fl6->flowlabel&IPV6_FLOWINFO_MASK) {
  571. if ((fl6->flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) {
  572. err = -EINVAL;
  573. goto exit_f;
  574. }
  575. }
  576. fl6->flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg);
  577. break;
  578. case IPV6_2292HOPOPTS:
  579. case IPV6_HOPOPTS:
  580. if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
  581. err = -EINVAL;
  582. goto exit_f;
  583. }
  584. hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
  585. len = ((hdr->hdrlen + 1) << 3);
  586. if (cmsg->cmsg_len < CMSG_LEN(len)) {
  587. err = -EINVAL;
  588. goto exit_f;
  589. }
  590. if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
  591. err = -EPERM;
  592. goto exit_f;
  593. }
  594. opt->opt_nflen += len;
  595. opt->hopopt = hdr;
  596. break;
  597. case IPV6_2292DSTOPTS:
  598. if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
  599. err = -EINVAL;
  600. goto exit_f;
  601. }
  602. hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
  603. len = ((hdr->hdrlen + 1) << 3);
  604. if (cmsg->cmsg_len < CMSG_LEN(len)) {
  605. err = -EINVAL;
  606. goto exit_f;
  607. }
  608. if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
  609. err = -EPERM;
  610. goto exit_f;
  611. }
  612. if (opt->dst1opt) {
  613. err = -EINVAL;
  614. goto exit_f;
  615. }
  616. opt->opt_flen += len;
  617. opt->dst1opt = hdr;
  618. break;
  619. case IPV6_DSTOPTS:
  620. case IPV6_RTHDRDSTOPTS:
  621. if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
  622. err = -EINVAL;
  623. goto exit_f;
  624. }
  625. hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
  626. len = ((hdr->hdrlen + 1) << 3);
  627. if (cmsg->cmsg_len < CMSG_LEN(len)) {
  628. err = -EINVAL;
  629. goto exit_f;
  630. }
  631. if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
  632. err = -EPERM;
  633. goto exit_f;
  634. }
  635. if (cmsg->cmsg_type == IPV6_DSTOPTS) {
  636. opt->opt_flen += len;
  637. opt->dst1opt = hdr;
  638. } else {
  639. opt->opt_nflen += len;
  640. opt->dst0opt = hdr;
  641. }
  642. break;
  643. case IPV6_2292RTHDR:
  644. case IPV6_RTHDR:
  645. if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) {
  646. err = -EINVAL;
  647. goto exit_f;
  648. }
  649. rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg);
  650. switch (rthdr->type) {
  651. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  652. case IPV6_SRCRT_TYPE_2:
  653. if (rthdr->hdrlen != 2 ||
  654. rthdr->segments_left != 1) {
  655. err = -EINVAL;
  656. goto exit_f;
  657. }
  658. break;
  659. #endif
  660. default:
  661. err = -EINVAL;
  662. goto exit_f;
  663. }
  664. len = ((rthdr->hdrlen + 1) << 3);
  665. if (cmsg->cmsg_len < CMSG_LEN(len)) {
  666. err = -EINVAL;
  667. goto exit_f;
  668. }
  669. /* segments left must also match */
  670. if ((rthdr->hdrlen >> 1) != rthdr->segments_left) {
  671. err = -EINVAL;
  672. goto exit_f;
  673. }
  674. opt->opt_nflen += len;
  675. opt->srcrt = rthdr;
  676. if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) {
  677. int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3);
  678. opt->opt_nflen += dsthdrlen;
  679. opt->dst0opt = opt->dst1opt;
  680. opt->dst1opt = NULL;
  681. opt->opt_flen -= dsthdrlen;
  682. }
  683. break;
  684. case IPV6_2292HOPLIMIT:
  685. case IPV6_HOPLIMIT:
  686. if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
  687. err = -EINVAL;
  688. goto exit_f;
  689. }
  690. *hlimit = *(int *)CMSG_DATA(cmsg);
  691. if (*hlimit < -1 || *hlimit > 0xff) {
  692. err = -EINVAL;
  693. goto exit_f;
  694. }
  695. break;
  696. case IPV6_TCLASS:
  697. {
  698. int tc;
  699. err = -EINVAL;
  700. if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
  701. goto exit_f;
  702. tc = *(int *)CMSG_DATA(cmsg);
  703. if (tc < -1 || tc > 0xff)
  704. goto exit_f;
  705. err = 0;
  706. *tclass = tc;
  707. break;
  708. }
  709. case IPV6_DONTFRAG:
  710. {
  711. int df;
  712. err = -EINVAL;
  713. if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
  714. goto exit_f;
  715. df = *(int *)CMSG_DATA(cmsg);
  716. if (df < 0 || df > 1)
  717. goto exit_f;
  718. err = 0;
  719. *dontfrag = df;
  720. break;
  721. }
  722. default:
  723. LIMIT_NETDEBUG(KERN_DEBUG "invalid cmsg type: %d\n",
  724. cmsg->cmsg_type);
  725. err = -EINVAL;
  726. goto exit_f;
  727. }
  728. }
  729. exit_f:
  730. return err;
  731. }
  732. EXPORT_SYMBOL_GPL(ip6_datagram_send_ctl);