ipv6.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. /* SCTP kernel implementation
  2. * (C) Copyright IBM Corp. 2002, 2004
  3. * Copyright (c) 2001 Nokia, Inc.
  4. * Copyright (c) 2001 La Monte H.P. Yarroll
  5. * Copyright (c) 2002-2003 Intel Corp.
  6. *
  7. * This file is part of the SCTP kernel implementation
  8. *
  9. * SCTP over IPv6.
  10. *
  11. * This SCTP implementation is free software;
  12. * you can redistribute it and/or modify it under the terms of
  13. * the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * This SCTP implementation is distributed in the hope that it
  18. * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  19. * ************************
  20. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  21. * See the GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with GNU CC; see the file COPYING. If not, see
  25. * <http://www.gnu.org/licenses/>.
  26. *
  27. * Please send any bug reports or fixes you make to the
  28. * email address(es):
  29. * lksctp developers <linux-sctp@vger.kernel.org>
  30. *
  31. * Written or modified by:
  32. * Le Yanqun <yanqun.le@nokia.com>
  33. * Hui Huang <hui.huang@nokia.com>
  34. * La Monte H.P. Yarroll <piggy@acm.org>
  35. * Sridhar Samudrala <sri@us.ibm.com>
  36. * Jon Grimm <jgrimm@us.ibm.com>
  37. * Ardelle Fan <ardelle.fan@intel.com>
  38. *
  39. * Based on:
  40. * linux/net/ipv6/tcp_ipv6.c
  41. */
  42. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  43. #include <linux/module.h>
  44. #include <linux/errno.h>
  45. #include <linux/types.h>
  46. #include <linux/socket.h>
  47. #include <linux/sockios.h>
  48. #include <linux/net.h>
  49. #include <linux/in.h>
  50. #include <linux/in6.h>
  51. #include <linux/netdevice.h>
  52. #include <linux/init.h>
  53. #include <linux/ipsec.h>
  54. #include <linux/slab.h>
  55. #include <linux/ipv6.h>
  56. #include <linux/icmpv6.h>
  57. #include <linux/random.h>
  58. #include <linux/seq_file.h>
  59. #include <net/protocol.h>
  60. #include <net/ndisc.h>
  61. #include <net/ip.h>
  62. #include <net/ipv6.h>
  63. #include <net/transp_v6.h>
  64. #include <net/addrconf.h>
  65. #include <net/ip6_route.h>
  66. #include <net/inet_common.h>
  67. #include <net/inet_ecn.h>
  68. #include <net/sctp/sctp.h>
  69. #include <asm/uaccess.h>
  70. static inline int sctp_v6_addr_match_len(union sctp_addr *s1,
  71. union sctp_addr *s2);
  72. static void sctp_v6_to_addr(union sctp_addr *addr, struct in6_addr *saddr,
  73. __be16 port);
  74. static int sctp_v6_cmp_addr(const union sctp_addr *addr1,
  75. const union sctp_addr *addr2);
  76. /* Event handler for inet6 address addition/deletion events.
  77. * The sctp_local_addr_list needs to be protocted by a spin lock since
  78. * multiple notifiers (say IPv4 and IPv6) may be running at the same
  79. * time and thus corrupt the list.
  80. * The reader side is protected with RCU.
  81. */
  82. static int sctp_inet6addr_event(struct notifier_block *this, unsigned long ev,
  83. void *ptr)
  84. {
  85. struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
  86. struct sctp_sockaddr_entry *addr = NULL;
  87. struct sctp_sockaddr_entry *temp;
  88. struct net *net = dev_net(ifa->idev->dev);
  89. int found = 0;
  90. switch (ev) {
  91. case NETDEV_UP:
  92. addr = kzalloc(sizeof(*addr), GFP_ATOMIC);
  93. if (addr) {
  94. addr->a.v6.sin6_family = AF_INET6;
  95. addr->a.v6.sin6_addr = ifa->addr;
  96. addr->a.v6.sin6_scope_id = ifa->idev->dev->ifindex;
  97. addr->valid = 1;
  98. spin_lock_bh(&net->sctp.local_addr_lock);
  99. list_add_tail_rcu(&addr->list, &net->sctp.local_addr_list);
  100. sctp_addr_wq_mgmt(net, addr, SCTP_ADDR_NEW);
  101. spin_unlock_bh(&net->sctp.local_addr_lock);
  102. }
  103. break;
  104. case NETDEV_DOWN:
  105. spin_lock_bh(&net->sctp.local_addr_lock);
  106. list_for_each_entry_safe(addr, temp,
  107. &net->sctp.local_addr_list, list) {
  108. if (addr->a.sa.sa_family == AF_INET6 &&
  109. ipv6_addr_equal(&addr->a.v6.sin6_addr,
  110. &ifa->addr)) {
  111. sctp_addr_wq_mgmt(net, addr, SCTP_ADDR_DEL);
  112. found = 1;
  113. addr->valid = 0;
  114. list_del_rcu(&addr->list);
  115. break;
  116. }
  117. }
  118. spin_unlock_bh(&net->sctp.local_addr_lock);
  119. if (found)
  120. kfree_rcu(addr, rcu);
  121. break;
  122. }
  123. return NOTIFY_DONE;
  124. }
  125. static struct notifier_block sctp_inet6addr_notifier = {
  126. .notifier_call = sctp_inet6addr_event,
  127. };
  128. /* ICMP error handler. */
  129. static void sctp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  130. u8 type, u8 code, int offset, __be32 info)
  131. {
  132. struct inet6_dev *idev;
  133. struct sock *sk;
  134. struct sctp_association *asoc;
  135. struct sctp_transport *transport;
  136. struct ipv6_pinfo *np;
  137. __u16 saveip, savesctp;
  138. int err;
  139. struct net *net = dev_net(skb->dev);
  140. idev = in6_dev_get(skb->dev);
  141. /* Fix up skb to look at the embedded net header. */
  142. saveip = skb->network_header;
  143. savesctp = skb->transport_header;
  144. skb_reset_network_header(skb);
  145. skb_set_transport_header(skb, offset);
  146. sk = sctp_err_lookup(net, AF_INET6, skb, sctp_hdr(skb), &asoc, &transport);
  147. /* Put back, the original pointers. */
  148. skb->network_header = saveip;
  149. skb->transport_header = savesctp;
  150. if (!sk) {
  151. ICMP6_INC_STATS_BH(net, idev, ICMP6_MIB_INERRORS);
  152. goto out;
  153. }
  154. /* Warning: The sock lock is held. Remember to call
  155. * sctp_err_finish!
  156. */
  157. switch (type) {
  158. case ICMPV6_PKT_TOOBIG:
  159. if (ip6_sk_accept_pmtu(sk))
  160. sctp_icmp_frag_needed(sk, asoc, transport, ntohl(info));
  161. goto out_unlock;
  162. case ICMPV6_PARAMPROB:
  163. if (ICMPV6_UNK_NEXTHDR == code) {
  164. sctp_icmp_proto_unreachable(sk, asoc, transport);
  165. goto out_unlock;
  166. }
  167. break;
  168. case NDISC_REDIRECT:
  169. sctp_icmp_redirect(sk, transport, skb);
  170. goto out_unlock;
  171. default:
  172. break;
  173. }
  174. np = inet6_sk(sk);
  175. icmpv6_err_convert(type, code, &err);
  176. if (!sock_owned_by_user(sk) && np->recverr) {
  177. sk->sk_err = err;
  178. sk->sk_error_report(sk);
  179. } else { /* Only an error on timeout */
  180. sk->sk_err_soft = err;
  181. }
  182. out_unlock:
  183. sctp_err_finish(sk, asoc);
  184. out:
  185. if (likely(idev != NULL))
  186. in6_dev_put(idev);
  187. }
  188. static int sctp_v6_xmit(struct sk_buff *skb, struct sctp_transport *transport)
  189. {
  190. struct sock *sk = skb->sk;
  191. struct ipv6_pinfo *np = inet6_sk(sk);
  192. struct flowi6 *fl6 = &transport->fl.u.ip6;
  193. int res;
  194. pr_debug("%s: skb:%p, len:%d, src:%pI6 dst:%pI6\n", __func__, skb,
  195. skb->len, &fl6->saddr, &fl6->daddr);
  196. IP6_ECN_flow_xmit(sk, fl6->flowlabel);
  197. if (!(transport->param_flags & SPP_PMTUD_ENABLE))
  198. skb->ignore_df = 1;
  199. SCTP_INC_STATS(sock_net(sk), SCTP_MIB_OUTSCTPPACKS);
  200. rcu_read_lock();
  201. res = ip6_xmit(sk, skb, fl6, rcu_dereference(np->opt), np->tclass);
  202. rcu_read_unlock();
  203. return res;
  204. }
  205. /* Returns the dst cache entry for the given source and destination ip
  206. * addresses.
  207. */
  208. static void sctp_v6_get_dst(struct sctp_transport *t, union sctp_addr *saddr,
  209. struct flowi *fl, struct sock *sk)
  210. {
  211. struct sctp_association *asoc = t->asoc;
  212. struct dst_entry *dst = NULL;
  213. struct flowi6 *fl6 = &fl->u.ip6;
  214. struct sctp_bind_addr *bp;
  215. struct ipv6_pinfo *np = inet6_sk(sk);
  216. struct sctp_sockaddr_entry *laddr;
  217. union sctp_addr *daddr = &t->ipaddr;
  218. union sctp_addr dst_saddr;
  219. struct in6_addr *final_p, final;
  220. __u8 matchlen = 0;
  221. sctp_scope_t scope;
  222. memset(fl6, 0, sizeof(struct flowi6));
  223. fl6->daddr = daddr->v6.sin6_addr;
  224. fl6->fl6_dport = daddr->v6.sin6_port;
  225. fl6->flowi6_proto = IPPROTO_SCTP;
  226. if (ipv6_addr_type(&daddr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
  227. fl6->flowi6_oif = daddr->v6.sin6_scope_id;
  228. pr_debug("%s: dst=%pI6 ", __func__, &fl6->daddr);
  229. if (asoc)
  230. fl6->fl6_sport = htons(asoc->base.bind_addr.port);
  231. if (saddr) {
  232. fl6->saddr = saddr->v6.sin6_addr;
  233. fl6->fl6_sport = saddr->v6.sin6_port;
  234. pr_debug("src=%pI6 - ", &fl6->saddr);
  235. }
  236. rcu_read_lock();
  237. final_p = fl6_update_dst(fl6, rcu_dereference(np->opt), &final);
  238. rcu_read_unlock();
  239. dst = ip6_dst_lookup_flow(sk, fl6, final_p);
  240. if (!asoc || saddr)
  241. goto out;
  242. bp = &asoc->base.bind_addr;
  243. scope = sctp_scope(daddr);
  244. /* ip6_dst_lookup has filled in the fl6->saddr for us. Check
  245. * to see if we can use it.
  246. */
  247. if (!IS_ERR(dst)) {
  248. /* Walk through the bind address list and look for a bind
  249. * address that matches the source address of the returned dst.
  250. */
  251. sctp_v6_to_addr(&dst_saddr, &fl6->saddr, htons(bp->port));
  252. rcu_read_lock();
  253. list_for_each_entry_rcu(laddr, &bp->address_list, list) {
  254. if (!laddr->valid || laddr->state == SCTP_ADDR_DEL ||
  255. (laddr->state != SCTP_ADDR_SRC &&
  256. !asoc->src_out_of_asoc_ok))
  257. continue;
  258. /* Do not compare against v4 addrs */
  259. if ((laddr->a.sa.sa_family == AF_INET6) &&
  260. (sctp_v6_cmp_addr(&dst_saddr, &laddr->a))) {
  261. rcu_read_unlock();
  262. goto out;
  263. }
  264. }
  265. rcu_read_unlock();
  266. /* None of the bound addresses match the source address of the
  267. * dst. So release it.
  268. */
  269. dst_release(dst);
  270. dst = NULL;
  271. }
  272. /* Walk through the bind address list and try to get the
  273. * best source address for a given destination.
  274. */
  275. rcu_read_lock();
  276. list_for_each_entry_rcu(laddr, &bp->address_list, list) {
  277. struct dst_entry *bdst;
  278. __u8 bmatchlen;
  279. if (!laddr->valid ||
  280. laddr->state != SCTP_ADDR_SRC ||
  281. laddr->a.sa.sa_family != AF_INET6 ||
  282. scope > sctp_scope(&laddr->a))
  283. continue;
  284. fl6->saddr = laddr->a.v6.sin6_addr;
  285. fl6->fl6_sport = laddr->a.v6.sin6_port;
  286. final_p = fl6_update_dst(fl6, rcu_dereference(np->opt), &final);
  287. bdst = ip6_dst_lookup_flow(sk, fl6, final_p);
  288. if (IS_ERR(bdst))
  289. continue;
  290. if (ipv6_chk_addr(dev_net(bdst->dev),
  291. &laddr->a.v6.sin6_addr, bdst->dev, 1)) {
  292. if (!IS_ERR_OR_NULL(dst))
  293. dst_release(dst);
  294. dst = bdst;
  295. break;
  296. }
  297. bmatchlen = sctp_v6_addr_match_len(daddr, &laddr->a);
  298. if (matchlen > bmatchlen) {
  299. dst_release(bdst);
  300. continue;
  301. }
  302. if (!IS_ERR_OR_NULL(dst))
  303. dst_release(dst);
  304. dst = bdst;
  305. matchlen = bmatchlen;
  306. }
  307. rcu_read_unlock();
  308. out:
  309. if (!IS_ERR_OR_NULL(dst)) {
  310. struct rt6_info *rt;
  311. rt = (struct rt6_info *)dst;
  312. t->dst = dst;
  313. t->dst_cookie = rt6_get_cookie(rt);
  314. pr_debug("rt6_dst:%pI6/%d rt6_src:%pI6\n",
  315. &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
  316. &fl6->saddr);
  317. } else {
  318. t->dst = NULL;
  319. pr_debug("no route\n");
  320. }
  321. }
  322. /* Returns the number of consecutive initial bits that match in the 2 ipv6
  323. * addresses.
  324. */
  325. static inline int sctp_v6_addr_match_len(union sctp_addr *s1,
  326. union sctp_addr *s2)
  327. {
  328. return ipv6_addr_diff(&s1->v6.sin6_addr, &s2->v6.sin6_addr);
  329. }
  330. /* Fills in the source address(saddr) based on the destination address(daddr)
  331. * and asoc's bind address list.
  332. */
  333. static void sctp_v6_get_saddr(struct sctp_sock *sk,
  334. struct sctp_transport *t,
  335. struct flowi *fl)
  336. {
  337. struct flowi6 *fl6 = &fl->u.ip6;
  338. union sctp_addr *saddr = &t->saddr;
  339. pr_debug("%s: asoc:%p dst:%p\n", __func__, t->asoc, t->dst);
  340. if (t->dst) {
  341. saddr->v6.sin6_family = AF_INET6;
  342. saddr->v6.sin6_addr = fl6->saddr;
  343. }
  344. }
  345. /* Make a copy of all potential local addresses. */
  346. static void sctp_v6_copy_addrlist(struct list_head *addrlist,
  347. struct net_device *dev)
  348. {
  349. struct inet6_dev *in6_dev;
  350. struct inet6_ifaddr *ifp;
  351. struct sctp_sockaddr_entry *addr;
  352. rcu_read_lock();
  353. if ((in6_dev = __in6_dev_get(dev)) == NULL) {
  354. rcu_read_unlock();
  355. return;
  356. }
  357. read_lock_bh(&in6_dev->lock);
  358. list_for_each_entry(ifp, &in6_dev->addr_list, if_list) {
  359. /* Add the address to the local list. */
  360. addr = kzalloc(sizeof(*addr), GFP_ATOMIC);
  361. if (addr) {
  362. addr->a.v6.sin6_family = AF_INET6;
  363. addr->a.v6.sin6_addr = ifp->addr;
  364. addr->a.v6.sin6_scope_id = dev->ifindex;
  365. addr->valid = 1;
  366. INIT_LIST_HEAD(&addr->list);
  367. list_add_tail(&addr->list, addrlist);
  368. }
  369. }
  370. read_unlock_bh(&in6_dev->lock);
  371. rcu_read_unlock();
  372. }
  373. /* Initialize a sockaddr_storage from in incoming skb. */
  374. static void sctp_v6_from_skb(union sctp_addr *addr, struct sk_buff *skb,
  375. int is_saddr)
  376. {
  377. __be16 *port;
  378. struct sctphdr *sh;
  379. port = &addr->v6.sin6_port;
  380. addr->v6.sin6_family = AF_INET6;
  381. addr->v6.sin6_flowinfo = 0; /* FIXME */
  382. addr->v6.sin6_scope_id = ((struct inet6_skb_parm *)skb->cb)->iif;
  383. sh = sctp_hdr(skb);
  384. if (is_saddr) {
  385. *port = sh->source;
  386. addr->v6.sin6_addr = ipv6_hdr(skb)->saddr;
  387. } else {
  388. *port = sh->dest;
  389. addr->v6.sin6_addr = ipv6_hdr(skb)->daddr;
  390. }
  391. }
  392. /* Initialize an sctp_addr from a socket. */
  393. static void sctp_v6_from_sk(union sctp_addr *addr, struct sock *sk)
  394. {
  395. addr->v6.sin6_family = AF_INET6;
  396. addr->v6.sin6_port = 0;
  397. addr->v6.sin6_addr = sk->sk_v6_rcv_saddr;
  398. }
  399. /* Initialize sk->sk_rcv_saddr from sctp_addr. */
  400. static void sctp_v6_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
  401. {
  402. if (addr->sa.sa_family == AF_INET) {
  403. sk->sk_v6_rcv_saddr.s6_addr32[0] = 0;
  404. sk->sk_v6_rcv_saddr.s6_addr32[1] = 0;
  405. sk->sk_v6_rcv_saddr.s6_addr32[2] = htonl(0x0000ffff);
  406. sk->sk_v6_rcv_saddr.s6_addr32[3] =
  407. addr->v4.sin_addr.s_addr;
  408. } else {
  409. sk->sk_v6_rcv_saddr = addr->v6.sin6_addr;
  410. }
  411. }
  412. /* Initialize sk->sk_daddr from sctp_addr. */
  413. static void sctp_v6_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
  414. {
  415. if (addr->sa.sa_family == AF_INET) {
  416. sk->sk_v6_daddr.s6_addr32[0] = 0;
  417. sk->sk_v6_daddr.s6_addr32[1] = 0;
  418. sk->sk_v6_daddr.s6_addr32[2] = htonl(0x0000ffff);
  419. sk->sk_v6_daddr.s6_addr32[3] = addr->v4.sin_addr.s_addr;
  420. } else {
  421. sk->sk_v6_daddr = addr->v6.sin6_addr;
  422. }
  423. }
  424. /* Initialize a sctp_addr from an address parameter. */
  425. static void sctp_v6_from_addr_param(union sctp_addr *addr,
  426. union sctp_addr_param *param,
  427. __be16 port, int iif)
  428. {
  429. addr->v6.sin6_family = AF_INET6;
  430. addr->v6.sin6_port = port;
  431. addr->v6.sin6_flowinfo = 0; /* BUG */
  432. addr->v6.sin6_addr = param->v6.addr;
  433. addr->v6.sin6_scope_id = iif;
  434. }
  435. /* Initialize an address parameter from a sctp_addr and return the length
  436. * of the address parameter.
  437. */
  438. static int sctp_v6_to_addr_param(const union sctp_addr *addr,
  439. union sctp_addr_param *param)
  440. {
  441. int length = sizeof(sctp_ipv6addr_param_t);
  442. param->v6.param_hdr.type = SCTP_PARAM_IPV6_ADDRESS;
  443. param->v6.param_hdr.length = htons(length);
  444. param->v6.addr = addr->v6.sin6_addr;
  445. return length;
  446. }
  447. /* Initialize a sctp_addr from struct in6_addr. */
  448. static void sctp_v6_to_addr(union sctp_addr *addr, struct in6_addr *saddr,
  449. __be16 port)
  450. {
  451. addr->sa.sa_family = AF_INET6;
  452. addr->v6.sin6_port = port;
  453. addr->v6.sin6_flowinfo = 0;
  454. addr->v6.sin6_addr = *saddr;
  455. addr->v6.sin6_scope_id = 0;
  456. }
  457. static int __sctp_v6_cmp_addr(const union sctp_addr *addr1,
  458. const union sctp_addr *addr2)
  459. {
  460. if (addr1->sa.sa_family != addr2->sa.sa_family) {
  461. if (addr1->sa.sa_family == AF_INET &&
  462. addr2->sa.sa_family == AF_INET6 &&
  463. ipv6_addr_v4mapped(&addr2->v6.sin6_addr) &&
  464. addr2->v6.sin6_addr.s6_addr32[3] ==
  465. addr1->v4.sin_addr.s_addr)
  466. return 1;
  467. if (addr2->sa.sa_family == AF_INET &&
  468. addr1->sa.sa_family == AF_INET6 &&
  469. ipv6_addr_v4mapped(&addr1->v6.sin6_addr) &&
  470. addr1->v6.sin6_addr.s6_addr32[3] ==
  471. addr2->v4.sin_addr.s_addr)
  472. return 1;
  473. return 0;
  474. }
  475. if (!ipv6_addr_equal(&addr1->v6.sin6_addr, &addr2->v6.sin6_addr))
  476. return 0;
  477. /* If this is a linklocal address, compare the scope_id. */
  478. if ((ipv6_addr_type(&addr1->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL) &&
  479. addr1->v6.sin6_scope_id && addr2->v6.sin6_scope_id &&
  480. addr1->v6.sin6_scope_id != addr2->v6.sin6_scope_id)
  481. return 0;
  482. return 1;
  483. }
  484. /* Compare addresses exactly.
  485. * v4-mapped-v6 is also in consideration.
  486. */
  487. static int sctp_v6_cmp_addr(const union sctp_addr *addr1,
  488. const union sctp_addr *addr2)
  489. {
  490. return __sctp_v6_cmp_addr(addr1, addr2) &&
  491. addr1->v6.sin6_port == addr2->v6.sin6_port;
  492. }
  493. /* Initialize addr struct to INADDR_ANY. */
  494. static void sctp_v6_inaddr_any(union sctp_addr *addr, __be16 port)
  495. {
  496. memset(addr, 0x00, sizeof(union sctp_addr));
  497. addr->v6.sin6_family = AF_INET6;
  498. addr->v6.sin6_port = port;
  499. }
  500. /* Is this a wildcard address? */
  501. static int sctp_v6_is_any(const union sctp_addr *addr)
  502. {
  503. return ipv6_addr_any(&addr->v6.sin6_addr);
  504. }
  505. /* Should this be available for binding? */
  506. static int sctp_v6_available(union sctp_addr *addr, struct sctp_sock *sp)
  507. {
  508. int type;
  509. const struct in6_addr *in6 = (const struct in6_addr *)&addr->v6.sin6_addr;
  510. type = ipv6_addr_type(in6);
  511. if (IPV6_ADDR_ANY == type)
  512. return 1;
  513. if (type == IPV6_ADDR_MAPPED) {
  514. if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
  515. return 0;
  516. sctp_v6_map_v4(addr);
  517. return sctp_get_af_specific(AF_INET)->available(addr, sp);
  518. }
  519. if (!(type & IPV6_ADDR_UNICAST))
  520. return 0;
  521. return ipv6_chk_addr(sock_net(&sp->inet.sk), in6, NULL, 0);
  522. }
  523. /* This function checks if the address is a valid address to be used for
  524. * SCTP.
  525. *
  526. * Output:
  527. * Return 0 - If the address is a non-unicast or an illegal address.
  528. * Return 1 - If the address is a unicast.
  529. */
  530. static int sctp_v6_addr_valid(union sctp_addr *addr,
  531. struct sctp_sock *sp,
  532. const struct sk_buff *skb)
  533. {
  534. int ret = ipv6_addr_type(&addr->v6.sin6_addr);
  535. /* Support v4-mapped-v6 address. */
  536. if (ret == IPV6_ADDR_MAPPED) {
  537. /* Note: This routine is used in input, so v4-mapped-v6
  538. * are disallowed here when there is no sctp_sock.
  539. */
  540. if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
  541. return 0;
  542. sctp_v6_map_v4(addr);
  543. return sctp_get_af_specific(AF_INET)->addr_valid(addr, sp, skb);
  544. }
  545. /* Is this a non-unicast address */
  546. if (!(ret & IPV6_ADDR_UNICAST))
  547. return 0;
  548. return 1;
  549. }
  550. /* What is the scope of 'addr'? */
  551. static sctp_scope_t sctp_v6_scope(union sctp_addr *addr)
  552. {
  553. int v6scope;
  554. sctp_scope_t retval;
  555. /* The IPv6 scope is really a set of bit fields.
  556. * See IFA_* in <net/if_inet6.h>. Map to a generic SCTP scope.
  557. */
  558. v6scope = ipv6_addr_scope(&addr->v6.sin6_addr);
  559. switch (v6scope) {
  560. case IFA_HOST:
  561. retval = SCTP_SCOPE_LOOPBACK;
  562. break;
  563. case IFA_LINK:
  564. retval = SCTP_SCOPE_LINK;
  565. break;
  566. case IFA_SITE:
  567. retval = SCTP_SCOPE_PRIVATE;
  568. break;
  569. default:
  570. retval = SCTP_SCOPE_GLOBAL;
  571. break;
  572. }
  573. return retval;
  574. }
  575. /* Create and initialize a new sk for the socket to be returned by accept(). */
  576. static struct sock *sctp_v6_create_accept_sk(struct sock *sk,
  577. struct sctp_association *asoc)
  578. {
  579. struct sock *newsk;
  580. struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
  581. struct sctp6_sock *newsctp6sk;
  582. struct ipv6_txoptions *opt;
  583. newsk = sk_alloc(sock_net(sk), PF_INET6, GFP_KERNEL, sk->sk_prot, 0);
  584. if (!newsk)
  585. goto out;
  586. sock_init_data(NULL, newsk);
  587. sctp_copy_sock(newsk, sk, asoc);
  588. sock_reset_flag(sk, SOCK_ZAPPED);
  589. newsctp6sk = (struct sctp6_sock *)newsk;
  590. inet_sk(newsk)->pinet6 = &newsctp6sk->inet6;
  591. sctp_sk(newsk)->v4mapped = sctp_sk(sk)->v4mapped;
  592. newnp = inet6_sk(newsk);
  593. memcpy(newnp, np, sizeof(struct ipv6_pinfo));
  594. newnp->ipv6_mc_list = NULL;
  595. newnp->ipv6_ac_list = NULL;
  596. newnp->ipv6_fl_list = NULL;
  597. rcu_read_lock();
  598. opt = rcu_dereference(np->opt);
  599. if (opt)
  600. opt = ipv6_dup_options(newsk, opt);
  601. RCU_INIT_POINTER(newnp->opt, opt);
  602. rcu_read_unlock();
  603. /* Initialize sk's sport, dport, rcv_saddr and daddr for getsockname()
  604. * and getpeername().
  605. */
  606. sctp_v6_to_sk_daddr(&asoc->peer.primary_addr, newsk);
  607. newsk->sk_v6_rcv_saddr = sk->sk_v6_rcv_saddr;
  608. sk_refcnt_debug_inc(newsk);
  609. if (newsk->sk_prot->init(newsk)) {
  610. sk_common_release(newsk);
  611. newsk = NULL;
  612. }
  613. out:
  614. return newsk;
  615. }
  616. /* Format a sockaddr for return to user space. This makes sure the return is
  617. * AF_INET or AF_INET6 depending on the SCTP_I_WANT_MAPPED_V4_ADDR option.
  618. */
  619. static int sctp_v6_addr_to_user(struct sctp_sock *sp, union sctp_addr *addr)
  620. {
  621. if (sp->v4mapped) {
  622. if (addr->sa.sa_family == AF_INET)
  623. sctp_v4_map_v6(addr);
  624. } else {
  625. if (addr->sa.sa_family == AF_INET6 &&
  626. ipv6_addr_v4mapped(&addr->v6.sin6_addr))
  627. sctp_v6_map_v4(addr);
  628. }
  629. if (addr->sa.sa_family == AF_INET) {
  630. memset(addr->v4.sin_zero, 0, sizeof(addr->v4.sin_zero));
  631. return sizeof(struct sockaddr_in);
  632. }
  633. return sizeof(struct sockaddr_in6);
  634. }
  635. /* Where did this skb come from? */
  636. static int sctp_v6_skb_iif(const struct sk_buff *skb)
  637. {
  638. struct inet6_skb_parm *opt = (struct inet6_skb_parm *) skb->cb;
  639. return opt->iif;
  640. }
  641. /* Was this packet marked by Explicit Congestion Notification? */
  642. static int sctp_v6_is_ce(const struct sk_buff *skb)
  643. {
  644. return *((__u32 *)(ipv6_hdr(skb))) & htonl(1 << 20);
  645. }
  646. /* Dump the v6 addr to the seq file. */
  647. static void sctp_v6_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
  648. {
  649. seq_printf(seq, "%pI6 ", &addr->v6.sin6_addr);
  650. }
  651. static void sctp_v6_ecn_capable(struct sock *sk)
  652. {
  653. inet6_sk(sk)->tclass |= INET_ECN_ECT_0;
  654. }
  655. /* Initialize a PF_INET msgname from a ulpevent. */
  656. static void sctp_inet6_event_msgname(struct sctp_ulpevent *event,
  657. char *msgname, int *addrlen)
  658. {
  659. union sctp_addr *addr;
  660. struct sctp_association *asoc;
  661. union sctp_addr *paddr;
  662. if (!msgname)
  663. return;
  664. addr = (union sctp_addr *)msgname;
  665. asoc = event->asoc;
  666. paddr = &asoc->peer.primary_addr;
  667. if (paddr->sa.sa_family == AF_INET) {
  668. addr->v4.sin_family = AF_INET;
  669. addr->v4.sin_port = htons(asoc->peer.port);
  670. addr->v4.sin_addr = paddr->v4.sin_addr;
  671. } else {
  672. addr->v6.sin6_family = AF_INET6;
  673. addr->v6.sin6_flowinfo = 0;
  674. if (ipv6_addr_type(&paddr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
  675. addr->v6.sin6_scope_id = paddr->v6.sin6_scope_id;
  676. else
  677. addr->v6.sin6_scope_id = 0;
  678. addr->v6.sin6_port = htons(asoc->peer.port);
  679. addr->v6.sin6_addr = paddr->v6.sin6_addr;
  680. }
  681. *addrlen = sctp_v6_addr_to_user(sctp_sk(asoc->base.sk), addr);
  682. }
  683. /* Initialize a msg_name from an inbound skb. */
  684. static void sctp_inet6_skb_msgname(struct sk_buff *skb, char *msgname,
  685. int *addr_len)
  686. {
  687. union sctp_addr *addr;
  688. struct sctphdr *sh;
  689. if (!msgname)
  690. return;
  691. addr = (union sctp_addr *)msgname;
  692. sh = sctp_hdr(skb);
  693. if (ip_hdr(skb)->version == 4) {
  694. addr->v4.sin_family = AF_INET;
  695. addr->v4.sin_port = sh->source;
  696. addr->v4.sin_addr.s_addr = ip_hdr(skb)->saddr;
  697. } else {
  698. addr->v6.sin6_family = AF_INET6;
  699. addr->v6.sin6_flowinfo = 0;
  700. addr->v6.sin6_port = sh->source;
  701. addr->v6.sin6_addr = ipv6_hdr(skb)->saddr;
  702. if (ipv6_addr_type(&addr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL) {
  703. struct sctp_ulpevent *ev = sctp_skb2event(skb);
  704. addr->v6.sin6_scope_id = ev->iif;
  705. } else {
  706. addr->v6.sin6_scope_id = 0;
  707. }
  708. }
  709. *addr_len = sctp_v6_addr_to_user(sctp_sk(skb->sk), addr);
  710. }
  711. /* Do we support this AF? */
  712. static int sctp_inet6_af_supported(sa_family_t family, struct sctp_sock *sp)
  713. {
  714. switch (family) {
  715. case AF_INET6:
  716. return 1;
  717. /* v4-mapped-v6 addresses */
  718. case AF_INET:
  719. if (!__ipv6_only_sock(sctp_opt2sk(sp)))
  720. return 1;
  721. default:
  722. return 0;
  723. }
  724. }
  725. /* Address matching with wildcards allowed. This extra level
  726. * of indirection lets us choose whether a PF_INET6 should
  727. * disallow any v4 addresses if we so choose.
  728. */
  729. static int sctp_inet6_cmp_addr(const union sctp_addr *addr1,
  730. const union sctp_addr *addr2,
  731. struct sctp_sock *opt)
  732. {
  733. struct sock *sk = sctp_opt2sk(opt);
  734. struct sctp_af *af1, *af2;
  735. af1 = sctp_get_af_specific(addr1->sa.sa_family);
  736. af2 = sctp_get_af_specific(addr2->sa.sa_family);
  737. if (!af1 || !af2)
  738. return 0;
  739. /* If the socket is IPv6 only, v4 addrs will not match */
  740. if (__ipv6_only_sock(sk) && af1 != af2)
  741. return 0;
  742. /* Today, wildcard AF_INET/AF_INET6. */
  743. if (sctp_is_any(sk, addr1) || sctp_is_any(sk, addr2))
  744. return 1;
  745. if (addr1->sa.sa_family == AF_INET && addr2->sa.sa_family == AF_INET)
  746. return addr1->v4.sin_addr.s_addr == addr2->v4.sin_addr.s_addr;
  747. return __sctp_v6_cmp_addr(addr1, addr2);
  748. }
  749. /* Verify that the provided sockaddr looks bindable. Common verification,
  750. * has already been taken care of.
  751. */
  752. static int sctp_inet6_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
  753. {
  754. struct sctp_af *af;
  755. /* ASSERT: address family has already been verified. */
  756. if (addr->sa.sa_family != AF_INET6)
  757. af = sctp_get_af_specific(addr->sa.sa_family);
  758. else {
  759. int type = ipv6_addr_type(&addr->v6.sin6_addr);
  760. struct net_device *dev;
  761. if (type & IPV6_ADDR_LINKLOCAL) {
  762. struct net *net;
  763. if (!addr->v6.sin6_scope_id)
  764. return 0;
  765. net = sock_net(&opt->inet.sk);
  766. rcu_read_lock();
  767. dev = dev_get_by_index_rcu(net, addr->v6.sin6_scope_id);
  768. if (!dev ||
  769. !ipv6_chk_addr(net, &addr->v6.sin6_addr, dev, 0)) {
  770. rcu_read_unlock();
  771. return 0;
  772. }
  773. rcu_read_unlock();
  774. }
  775. af = opt->pf->af;
  776. }
  777. return af->available(addr, opt);
  778. }
  779. /* Verify that the provided sockaddr looks sendable. Common verification,
  780. * has already been taken care of.
  781. */
  782. static int sctp_inet6_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
  783. {
  784. struct sctp_af *af = NULL;
  785. /* ASSERT: address family has already been verified. */
  786. if (addr->sa.sa_family != AF_INET6)
  787. af = sctp_get_af_specific(addr->sa.sa_family);
  788. else {
  789. int type = ipv6_addr_type(&addr->v6.sin6_addr);
  790. struct net_device *dev;
  791. if (type & IPV6_ADDR_LINKLOCAL) {
  792. if (!addr->v6.sin6_scope_id)
  793. return 0;
  794. rcu_read_lock();
  795. dev = dev_get_by_index_rcu(sock_net(&opt->inet.sk),
  796. addr->v6.sin6_scope_id);
  797. rcu_read_unlock();
  798. if (!dev)
  799. return 0;
  800. }
  801. af = opt->pf->af;
  802. }
  803. return af != NULL;
  804. }
  805. /* Fill in Supported Address Type information for INIT and INIT-ACK
  806. * chunks. Note: In the future, we may want to look at sock options
  807. * to determine whether a PF_INET6 socket really wants to have IPV4
  808. * addresses.
  809. * Returns number of addresses supported.
  810. */
  811. static int sctp_inet6_supported_addrs(const struct sctp_sock *opt,
  812. __be16 *types)
  813. {
  814. types[0] = SCTP_PARAM_IPV6_ADDRESS;
  815. if (!opt || !ipv6_only_sock(sctp_opt2sk(opt))) {
  816. types[1] = SCTP_PARAM_IPV4_ADDRESS;
  817. return 2;
  818. }
  819. return 1;
  820. }
  821. /* Handle SCTP_I_WANT_MAPPED_V4_ADDR for getpeername() and getsockname() */
  822. static int sctp_getname(struct socket *sock, struct sockaddr *uaddr,
  823. int *uaddr_len, int peer)
  824. {
  825. int rc;
  826. rc = inet6_getname(sock, uaddr, uaddr_len, peer);
  827. if (rc != 0)
  828. return rc;
  829. *uaddr_len = sctp_v6_addr_to_user(sctp_sk(sock->sk),
  830. (union sctp_addr *)uaddr);
  831. return rc;
  832. }
  833. static const struct proto_ops inet6_seqpacket_ops = {
  834. .family = PF_INET6,
  835. .owner = THIS_MODULE,
  836. .release = inet6_release,
  837. .bind = inet6_bind,
  838. .connect = inet_dgram_connect,
  839. .socketpair = sock_no_socketpair,
  840. .accept = inet_accept,
  841. .getname = sctp_getname,
  842. .poll = sctp_poll,
  843. .ioctl = inet6_ioctl,
  844. .listen = sctp_inet_listen,
  845. .shutdown = inet_shutdown,
  846. .setsockopt = sock_common_setsockopt,
  847. .getsockopt = sock_common_getsockopt,
  848. .sendmsg = inet_sendmsg,
  849. .recvmsg = sock_common_recvmsg,
  850. .mmap = sock_no_mmap,
  851. #ifdef CONFIG_COMPAT
  852. .compat_setsockopt = compat_sock_common_setsockopt,
  853. .compat_getsockopt = compat_sock_common_getsockopt,
  854. #endif
  855. };
  856. static struct inet_protosw sctpv6_seqpacket_protosw = {
  857. .type = SOCK_SEQPACKET,
  858. .protocol = IPPROTO_SCTP,
  859. .prot = &sctpv6_prot,
  860. .ops = &inet6_seqpacket_ops,
  861. .flags = SCTP_PROTOSW_FLAG
  862. };
  863. static struct inet_protosw sctpv6_stream_protosw = {
  864. .type = SOCK_STREAM,
  865. .protocol = IPPROTO_SCTP,
  866. .prot = &sctpv6_prot,
  867. .ops = &inet6_seqpacket_ops,
  868. .flags = SCTP_PROTOSW_FLAG,
  869. };
  870. static int sctp6_rcv(struct sk_buff *skb)
  871. {
  872. return sctp_rcv(skb) ? -1 : 0;
  873. }
  874. static const struct inet6_protocol sctpv6_protocol = {
  875. .handler = sctp6_rcv,
  876. .err_handler = sctp_v6_err,
  877. .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL,
  878. };
  879. static struct sctp_af sctp_af_inet6 = {
  880. .sa_family = AF_INET6,
  881. .sctp_xmit = sctp_v6_xmit,
  882. .setsockopt = ipv6_setsockopt,
  883. .getsockopt = ipv6_getsockopt,
  884. .get_dst = sctp_v6_get_dst,
  885. .get_saddr = sctp_v6_get_saddr,
  886. .copy_addrlist = sctp_v6_copy_addrlist,
  887. .from_skb = sctp_v6_from_skb,
  888. .from_sk = sctp_v6_from_sk,
  889. .from_addr_param = sctp_v6_from_addr_param,
  890. .to_addr_param = sctp_v6_to_addr_param,
  891. .cmp_addr = sctp_v6_cmp_addr,
  892. .scope = sctp_v6_scope,
  893. .addr_valid = sctp_v6_addr_valid,
  894. .inaddr_any = sctp_v6_inaddr_any,
  895. .is_any = sctp_v6_is_any,
  896. .available = sctp_v6_available,
  897. .skb_iif = sctp_v6_skb_iif,
  898. .is_ce = sctp_v6_is_ce,
  899. .seq_dump_addr = sctp_v6_seq_dump_addr,
  900. .ecn_capable = sctp_v6_ecn_capable,
  901. .net_header_len = sizeof(struct ipv6hdr),
  902. .sockaddr_len = sizeof(struct sockaddr_in6),
  903. #ifdef CONFIG_COMPAT
  904. .compat_setsockopt = compat_ipv6_setsockopt,
  905. .compat_getsockopt = compat_ipv6_getsockopt,
  906. #endif
  907. };
  908. static struct sctp_pf sctp_pf_inet6 = {
  909. .event_msgname = sctp_inet6_event_msgname,
  910. .skb_msgname = sctp_inet6_skb_msgname,
  911. .af_supported = sctp_inet6_af_supported,
  912. .cmp_addr = sctp_inet6_cmp_addr,
  913. .bind_verify = sctp_inet6_bind_verify,
  914. .send_verify = sctp_inet6_send_verify,
  915. .supported_addrs = sctp_inet6_supported_addrs,
  916. .create_accept_sk = sctp_v6_create_accept_sk,
  917. .addr_to_user = sctp_v6_addr_to_user,
  918. .to_sk_saddr = sctp_v6_to_sk_saddr,
  919. .to_sk_daddr = sctp_v6_to_sk_daddr,
  920. .af = &sctp_af_inet6,
  921. };
  922. /* Initialize IPv6 support and register with socket layer. */
  923. void sctp_v6_pf_init(void)
  924. {
  925. /* Register the SCTP specific PF_INET6 functions. */
  926. sctp_register_pf(&sctp_pf_inet6, PF_INET6);
  927. /* Register the SCTP specific AF_INET6 functions. */
  928. sctp_register_af(&sctp_af_inet6);
  929. }
  930. void sctp_v6_pf_exit(void)
  931. {
  932. list_del(&sctp_af_inet6.list);
  933. }
  934. /* Initialize IPv6 support and register with socket layer. */
  935. int sctp_v6_protosw_init(void)
  936. {
  937. int rc;
  938. rc = proto_register(&sctpv6_prot, 1);
  939. if (rc)
  940. return rc;
  941. /* Add SCTPv6(UDP and TCP style) to inetsw6 linked list. */
  942. inet6_register_protosw(&sctpv6_seqpacket_protosw);
  943. inet6_register_protosw(&sctpv6_stream_protosw);
  944. return 0;
  945. }
  946. void sctp_v6_protosw_exit(void)
  947. {
  948. inet6_unregister_protosw(&sctpv6_seqpacket_protosw);
  949. inet6_unregister_protosw(&sctpv6_stream_protosw);
  950. proto_unregister(&sctpv6_prot);
  951. }
  952. /* Register with inet6 layer. */
  953. int sctp_v6_add_protocol(void)
  954. {
  955. /* Register notifier for inet6 address additions/deletions. */
  956. register_inet6addr_notifier(&sctp_inet6addr_notifier);
  957. if (inet6_add_protocol(&sctpv6_protocol, IPPROTO_SCTP) < 0)
  958. return -EAGAIN;
  959. return 0;
  960. }
  961. /* Unregister with inet6 layer. */
  962. void sctp_v6_del_protocol(void)
  963. {
  964. inet6_del_protocol(&sctpv6_protocol, IPPROTO_SCTP);
  965. unregister_inet6addr_notifier(&sctp_inet6addr_notifier);
  966. }