datagram.c 20 KB

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