datagram.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  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 (addr_type&IPV6_ADDR_LINKLOCAL) {
  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. sin->sin6_scope_id = 0;
  294. if (skb->protocol == htons(ETH_P_IPV6)) {
  295. const struct ipv6hdr *ip6h = container_of((struct in6_addr *)(nh + serr->addr_offset),
  296. struct ipv6hdr, daddr);
  297. sin->sin6_addr = ip6h->daddr;
  298. if (np->sndflow)
  299. sin->sin6_flowinfo = ip6_flowinfo(ip6h);
  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_get_dsfield(ipv6_hdr(skb));
  406. put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
  407. }
  408. if (np->rxopt.bits.rxflow) {
  409. __be32 flowinfo = ip6_flowinfo((struct ipv6hdr *)nh);
  410. if (flowinfo)
  411. put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
  412. }
  413. /* HbH is allowed only once */
  414. if (np->rxopt.bits.hopopts && opt->hop) {
  415. u8 *ptr = nh + opt->hop;
  416. put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr);
  417. }
  418. if (opt->lastopt &&
  419. (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) {
  420. /*
  421. * Silly enough, but we need to reparse in order to
  422. * report extension headers (except for HbH)
  423. * in order.
  424. *
  425. * Also note that IPV6_RECVRTHDRDSTOPTS is NOT
  426. * (and WILL NOT be) defined because
  427. * IPV6_RECVDSTOPTS is more generic. --yoshfuji
  428. */
  429. unsigned int off = sizeof(struct ipv6hdr);
  430. u8 nexthdr = ipv6_hdr(skb)->nexthdr;
  431. while (off <= opt->lastopt) {
  432. unsigned int len;
  433. u8 *ptr = nh + off;
  434. switch (nexthdr) {
  435. case IPPROTO_DSTOPTS:
  436. nexthdr = ptr[0];
  437. len = (ptr[1] + 1) << 3;
  438. if (np->rxopt.bits.dstopts)
  439. put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr);
  440. break;
  441. case IPPROTO_ROUTING:
  442. nexthdr = ptr[0];
  443. len = (ptr[1] + 1) << 3;
  444. if (np->rxopt.bits.srcrt)
  445. put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr);
  446. break;
  447. case IPPROTO_AH:
  448. nexthdr = ptr[0];
  449. len = (ptr[1] + 2) << 2;
  450. break;
  451. default:
  452. nexthdr = ptr[0];
  453. len = (ptr[1] + 1) << 3;
  454. break;
  455. }
  456. off += len;
  457. }
  458. }
  459. /* socket options in old style */
  460. if (np->rxopt.bits.rxoinfo) {
  461. struct in6_pktinfo src_info;
  462. src_info.ipi6_ifindex = opt->iif;
  463. src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
  464. put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
  465. }
  466. if (np->rxopt.bits.rxohlim) {
  467. int hlim = ipv6_hdr(skb)->hop_limit;
  468. put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
  469. }
  470. if (np->rxopt.bits.ohopopts && opt->hop) {
  471. u8 *ptr = nh + opt->hop;
  472. put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr);
  473. }
  474. if (np->rxopt.bits.odstopts && opt->dst0) {
  475. u8 *ptr = nh + opt->dst0;
  476. put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
  477. }
  478. if (np->rxopt.bits.osrcrt && opt->srcrt) {
  479. struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt);
  480. put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr);
  481. }
  482. if (np->rxopt.bits.odstopts && opt->dst1) {
  483. u8 *ptr = nh + opt->dst1;
  484. put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
  485. }
  486. if (np->rxopt.bits.rxorigdstaddr) {
  487. struct sockaddr_in6 sin6;
  488. __be16 *ports = (__be16 *) skb_transport_header(skb);
  489. if (skb_transport_offset(skb) + 4 <= skb->len) {
  490. /* All current transport protocols have the port numbers in the
  491. * first four bytes of the transport header and this function is
  492. * written with this assumption in mind.
  493. */
  494. sin6.sin6_family = AF_INET6;
  495. sin6.sin6_addr = ipv6_hdr(skb)->daddr;
  496. sin6.sin6_port = ports[1];
  497. sin6.sin6_flowinfo = 0;
  498. sin6.sin6_scope_id = 0;
  499. put_cmsg(msg, SOL_IPV6, IPV6_ORIGDSTADDR, sizeof(sin6), &sin6);
  500. }
  501. }
  502. return 0;
  503. }
  504. EXPORT_SYMBOL_GPL(ip6_datagram_recv_ctl);
  505. int ip6_datagram_send_ctl(struct net *net, struct sock *sk,
  506. struct msghdr *msg, struct flowi6 *fl6,
  507. struct ipv6_txoptions *opt,
  508. int *hlimit, int *tclass, int *dontfrag)
  509. {
  510. struct in6_pktinfo *src_info;
  511. struct cmsghdr *cmsg;
  512. struct ipv6_rt_hdr *rthdr;
  513. struct ipv6_opt_hdr *hdr;
  514. int len;
  515. int err = 0;
  516. for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
  517. int addr_type;
  518. if (!CMSG_OK(msg, cmsg)) {
  519. err = -EINVAL;
  520. goto exit_f;
  521. }
  522. if (cmsg->cmsg_level != SOL_IPV6)
  523. continue;
  524. switch (cmsg->cmsg_type) {
  525. case IPV6_PKTINFO:
  526. case IPV6_2292PKTINFO:
  527. {
  528. struct net_device *dev = NULL;
  529. if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) {
  530. err = -EINVAL;
  531. goto exit_f;
  532. }
  533. src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
  534. if (src_info->ipi6_ifindex) {
  535. if (fl6->flowi6_oif &&
  536. src_info->ipi6_ifindex != fl6->flowi6_oif)
  537. return -EINVAL;
  538. fl6->flowi6_oif = src_info->ipi6_ifindex;
  539. }
  540. addr_type = __ipv6_addr_type(&src_info->ipi6_addr);
  541. rcu_read_lock();
  542. if (fl6->flowi6_oif) {
  543. dev = dev_get_by_index_rcu(net, fl6->flowi6_oif);
  544. if (!dev) {
  545. rcu_read_unlock();
  546. return -ENODEV;
  547. }
  548. } else if (addr_type & IPV6_ADDR_LINKLOCAL) {
  549. rcu_read_unlock();
  550. return -EINVAL;
  551. }
  552. if (addr_type != IPV6_ADDR_ANY) {
  553. int strict = __ipv6_addr_src_scope(addr_type) <= IPV6_ADDR_SCOPE_LINKLOCAL;
  554. if (!(inet_sk(sk)->freebind || inet_sk(sk)->transparent) &&
  555. !ipv6_chk_addr(net, &src_info->ipi6_addr,
  556. strict ? dev : NULL, 0))
  557. err = -EINVAL;
  558. else
  559. fl6->saddr = src_info->ipi6_addr;
  560. }
  561. rcu_read_unlock();
  562. if (err)
  563. goto exit_f;
  564. break;
  565. }
  566. case IPV6_FLOWINFO:
  567. if (cmsg->cmsg_len < CMSG_LEN(4)) {
  568. err = -EINVAL;
  569. goto exit_f;
  570. }
  571. if (fl6->flowlabel&IPV6_FLOWINFO_MASK) {
  572. if ((fl6->flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) {
  573. err = -EINVAL;
  574. goto exit_f;
  575. }
  576. }
  577. fl6->flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg);
  578. break;
  579. case IPV6_2292HOPOPTS:
  580. case IPV6_HOPOPTS:
  581. if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
  582. err = -EINVAL;
  583. goto exit_f;
  584. }
  585. hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
  586. len = ((hdr->hdrlen + 1) << 3);
  587. if (cmsg->cmsg_len < CMSG_LEN(len)) {
  588. err = -EINVAL;
  589. goto exit_f;
  590. }
  591. if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
  592. err = -EPERM;
  593. goto exit_f;
  594. }
  595. opt->opt_nflen += len;
  596. opt->hopopt = hdr;
  597. break;
  598. case IPV6_2292DSTOPTS:
  599. if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
  600. err = -EINVAL;
  601. goto exit_f;
  602. }
  603. hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
  604. len = ((hdr->hdrlen + 1) << 3);
  605. if (cmsg->cmsg_len < CMSG_LEN(len)) {
  606. err = -EINVAL;
  607. goto exit_f;
  608. }
  609. if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
  610. err = -EPERM;
  611. goto exit_f;
  612. }
  613. if (opt->dst1opt) {
  614. err = -EINVAL;
  615. goto exit_f;
  616. }
  617. opt->opt_flen += len;
  618. opt->dst1opt = hdr;
  619. break;
  620. case IPV6_DSTOPTS:
  621. case IPV6_RTHDRDSTOPTS:
  622. if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
  623. err = -EINVAL;
  624. goto exit_f;
  625. }
  626. hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
  627. len = ((hdr->hdrlen + 1) << 3);
  628. if (cmsg->cmsg_len < CMSG_LEN(len)) {
  629. err = -EINVAL;
  630. goto exit_f;
  631. }
  632. if (!ns_capable(net->user_ns, CAP_NET_RAW)) {
  633. err = -EPERM;
  634. goto exit_f;
  635. }
  636. if (cmsg->cmsg_type == IPV6_DSTOPTS) {
  637. opt->opt_flen += len;
  638. opt->dst1opt = hdr;
  639. } else {
  640. opt->opt_nflen += len;
  641. opt->dst0opt = hdr;
  642. }
  643. break;
  644. case IPV6_2292RTHDR:
  645. case IPV6_RTHDR:
  646. if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) {
  647. err = -EINVAL;
  648. goto exit_f;
  649. }
  650. rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg);
  651. switch (rthdr->type) {
  652. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  653. case IPV6_SRCRT_TYPE_2:
  654. if (rthdr->hdrlen != 2 ||
  655. rthdr->segments_left != 1) {
  656. err = -EINVAL;
  657. goto exit_f;
  658. }
  659. break;
  660. #endif
  661. default:
  662. err = -EINVAL;
  663. goto exit_f;
  664. }
  665. len = ((rthdr->hdrlen + 1) << 3);
  666. if (cmsg->cmsg_len < CMSG_LEN(len)) {
  667. err = -EINVAL;
  668. goto exit_f;
  669. }
  670. /* segments left must also match */
  671. if ((rthdr->hdrlen >> 1) != rthdr->segments_left) {
  672. err = -EINVAL;
  673. goto exit_f;
  674. }
  675. opt->opt_nflen += len;
  676. opt->srcrt = rthdr;
  677. if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) {
  678. int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3);
  679. opt->opt_nflen += dsthdrlen;
  680. opt->dst0opt = opt->dst1opt;
  681. opt->dst1opt = NULL;
  682. opt->opt_flen -= dsthdrlen;
  683. }
  684. break;
  685. case IPV6_2292HOPLIMIT:
  686. case IPV6_HOPLIMIT:
  687. if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
  688. err = -EINVAL;
  689. goto exit_f;
  690. }
  691. *hlimit = *(int *)CMSG_DATA(cmsg);
  692. if (*hlimit < -1 || *hlimit > 0xff) {
  693. err = -EINVAL;
  694. goto exit_f;
  695. }
  696. break;
  697. case IPV6_TCLASS:
  698. {
  699. int tc;
  700. err = -EINVAL;
  701. if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
  702. goto exit_f;
  703. tc = *(int *)CMSG_DATA(cmsg);
  704. if (tc < -1 || tc > 0xff)
  705. goto exit_f;
  706. err = 0;
  707. *tclass = tc;
  708. break;
  709. }
  710. case IPV6_DONTFRAG:
  711. {
  712. int df;
  713. err = -EINVAL;
  714. if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
  715. goto exit_f;
  716. df = *(int *)CMSG_DATA(cmsg);
  717. if (df < 0 || df > 1)
  718. goto exit_f;
  719. err = 0;
  720. *dontfrag = df;
  721. break;
  722. }
  723. default:
  724. LIMIT_NETDEBUG(KERN_DEBUG "invalid cmsg type: %d\n",
  725. cmsg->cmsg_type);
  726. err = -EINVAL;
  727. goto exit_f;
  728. }
  729. }
  730. exit_f:
  731. return err;
  732. }
  733. EXPORT_SYMBOL_GPL(ip6_datagram_send_ctl);