datagram.c 20 KB

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