ipv6.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  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 <[email protected]>
  30. *
  31. * Written or modified by:
  32. * Le Yanqun <[email protected]>
  33. * Hui Huang <[email protected]>
  34. * La Monte H.P. Yarroll <[email protected]>
  35. * Sridhar Samudrala <[email protected]>
  36. * Jon Grimm <[email protected]>
  37. * Ardelle Fan <[email protected]>
  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(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, transport);
  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, sk->sk_mark, rcu_dereference(np->opt),
  202. np->tclass);
  203. rcu_read_unlock();
  204. return res;
  205. }
  206. /* Returns the dst cache entry for the given source and destination ip
  207. * addresses.
  208. */
  209. static void sctp_v6_get_dst(struct sctp_transport *t, union sctp_addr *saddr,
  210. struct flowi *fl, struct sock *sk)
  211. {
  212. struct sctp_association *asoc = t->asoc;
  213. struct dst_entry *dst = NULL;
  214. struct flowi6 *fl6 = &fl->u.ip6;
  215. struct sctp_bind_addr *bp;
  216. struct ipv6_pinfo *np = inet6_sk(sk);
  217. struct sctp_sockaddr_entry *laddr;
  218. union sctp_addr *daddr = &t->ipaddr;
  219. union sctp_addr dst_saddr;
  220. struct in6_addr *final_p, final;
  221. __u8 matchlen = 0;
  222. sctp_scope_t scope;
  223. memset(fl6, 0, sizeof(struct flowi6));
  224. fl6->daddr = daddr->v6.sin6_addr;
  225. fl6->fl6_dport = daddr->v6.sin6_port;
  226. fl6->flowi6_proto = IPPROTO_SCTP;
  227. if (ipv6_addr_type(&daddr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
  228. fl6->flowi6_oif = daddr->v6.sin6_scope_id;
  229. pr_debug("%s: dst=%pI6 ", __func__, &fl6->daddr);
  230. if (asoc)
  231. fl6->fl6_sport = htons(asoc->base.bind_addr.port);
  232. if (saddr) {
  233. fl6->saddr = saddr->v6.sin6_addr;
  234. fl6->fl6_sport = saddr->v6.sin6_port;
  235. pr_debug("src=%pI6 - ", &fl6->saddr);
  236. }
  237. rcu_read_lock();
  238. final_p = fl6_update_dst(fl6, rcu_dereference(np->opt), &final);
  239. rcu_read_unlock();
  240. dst = ip6_dst_lookup_flow(sk, fl6, final_p);
  241. if (!asoc || saddr)
  242. goto out;
  243. bp = &asoc->base.bind_addr;
  244. scope = sctp_scope(daddr);
  245. /* ip6_dst_lookup has filled in the fl6->saddr for us. Check
  246. * to see if we can use it.
  247. */
  248. if (!IS_ERR(dst)) {
  249. /* Walk through the bind address list and look for a bind
  250. * address that matches the source address of the returned dst.
  251. */
  252. sctp_v6_to_addr(&dst_saddr, &fl6->saddr, htons(bp->port));
  253. rcu_read_lock();
  254. list_for_each_entry_rcu(laddr, &bp->address_list, list) {
  255. if (!laddr->valid || laddr->state == SCTP_ADDR_DEL ||
  256. (laddr->state != SCTP_ADDR_SRC &&
  257. !asoc->src_out_of_asoc_ok))
  258. continue;
  259. /* Do not compare against v4 addrs */
  260. if ((laddr->a.sa.sa_family == AF_INET6) &&
  261. (sctp_v6_cmp_addr(&dst_saddr, &laddr->a))) {
  262. rcu_read_unlock();
  263. goto out;
  264. }
  265. }
  266. rcu_read_unlock();
  267. /* None of the bound addresses match the source address of the
  268. * dst. So release it.
  269. */
  270. dst_release(dst);
  271. dst = NULL;
  272. }
  273. /* Walk through the bind address list and try to get the
  274. * best source address for a given destination.
  275. */
  276. rcu_read_lock();
  277. list_for_each_entry_rcu(laddr, &bp->address_list, list) {
  278. struct dst_entry *bdst;
  279. __u8 bmatchlen;
  280. if (!laddr->valid ||
  281. laddr->state != SCTP_ADDR_SRC ||
  282. laddr->a.sa.sa_family != AF_INET6 ||
  283. scope > sctp_scope(&laddr->a))
  284. continue;
  285. fl6->saddr = laddr->a.v6.sin6_addr;
  286. fl6->fl6_sport = laddr->a.v6.sin6_port;
  287. final_p = fl6_update_dst(fl6, rcu_dereference(np->opt), &final);
  288. bdst = ip6_dst_lookup_flow(sk, fl6, final_p);
  289. if (IS_ERR(bdst))
  290. continue;
  291. if (ipv6_chk_addr(dev_net(bdst->dev),
  292. &laddr->a.v6.sin6_addr, bdst->dev, 1)) {
  293. if (!IS_ERR_OR_NULL(dst))
  294. dst_release(dst);
  295. dst = bdst;
  296. break;
  297. }
  298. bmatchlen = sctp_v6_addr_match_len(daddr, &laddr->a);
  299. if (matchlen > bmatchlen) {
  300. dst_release(bdst);
  301. continue;
  302. }
  303. if (!IS_ERR_OR_NULL(dst))
  304. dst_release(dst);
  305. dst = bdst;
  306. matchlen = bmatchlen;
  307. }
  308. rcu_read_unlock();
  309. out:
  310. if (!IS_ERR_OR_NULL(dst)) {
  311. struct rt6_info *rt;
  312. rt = (struct rt6_info *)dst;
  313. t->dst = dst;
  314. t->dst_cookie = rt6_get_cookie(rt);
  315. pr_debug("rt6_dst:%pI6/%d rt6_src:%pI6\n",
  316. &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
  317. &fl6->saddr);
  318. } else {
  319. t->dst = NULL;
  320. pr_debug("no route\n");
  321. }
  322. }
  323. /* Returns the number of consecutive initial bits that match in the 2 ipv6
  324. * addresses.
  325. */
  326. static inline int sctp_v6_addr_match_len(union sctp_addr *s1,
  327. union sctp_addr *s2)
  328. {
  329. return ipv6_addr_diff(&s1->v6.sin6_addr, &s2->v6.sin6_addr);
  330. }
  331. /* Fills in the source address(saddr) based on the destination address(daddr)
  332. * and asoc's bind address list.
  333. */
  334. static void sctp_v6_get_saddr(struct sctp_sock *sk,
  335. struct sctp_transport *t,
  336. struct flowi *fl)
  337. {
  338. struct flowi6 *fl6 = &fl->u.ip6;
  339. union sctp_addr *saddr = &t->saddr;
  340. pr_debug("%s: asoc:%p dst:%p\n", __func__, t->asoc, t->dst);
  341. if (t->dst) {
  342. saddr->v6.sin6_family = AF_INET6;
  343. saddr->v6.sin6_addr = fl6->saddr;
  344. }
  345. }
  346. /* Make a copy of all potential local addresses. */
  347. static void sctp_v6_copy_addrlist(struct list_head *addrlist,
  348. struct net_device *dev)
  349. {
  350. struct inet6_dev *in6_dev;
  351. struct inet6_ifaddr *ifp;
  352. struct sctp_sockaddr_entry *addr;
  353. rcu_read_lock();
  354. if ((in6_dev = __in6_dev_get(dev)) == NULL) {
  355. rcu_read_unlock();
  356. return;
  357. }
  358. read_lock_bh(&in6_dev->lock);
  359. list_for_each_entry(ifp, &in6_dev->addr_list, if_list) {
  360. /* Add the address to the local list. */
  361. addr = kzalloc(sizeof(*addr), GFP_ATOMIC);
  362. if (addr) {
  363. addr->a.v6.sin6_family = AF_INET6;
  364. addr->a.v6.sin6_addr = ifp->addr;
  365. addr->a.v6.sin6_scope_id = dev->ifindex;
  366. addr->valid = 1;
  367. INIT_LIST_HEAD(&addr->list);
  368. list_add_tail(&addr->list, addrlist);
  369. }
  370. }
  371. read_unlock_bh(&in6_dev->lock);
  372. rcu_read_unlock();
  373. }
  374. /* Initialize a sockaddr_storage from in incoming skb. */
  375. static void sctp_v6_from_skb(union sctp_addr *addr, struct sk_buff *skb,
  376. int is_saddr)
  377. {
  378. __be16 *port;
  379. struct sctphdr *sh;
  380. port = &addr->v6.sin6_port;
  381. addr->v6.sin6_family = AF_INET6;
  382. addr->v6.sin6_flowinfo = 0; /* FIXME */
  383. addr->v6.sin6_scope_id = ((struct inet6_skb_parm *)skb->cb)->iif;
  384. /* Always called on head skb, so this is safe */
  385. sh = sctp_hdr(skb);
  386. if (is_saddr) {
  387. *port = sh->source;
  388. addr->v6.sin6_addr = ipv6_hdr(skb)->saddr;
  389. } else {
  390. *port = sh->dest;
  391. addr->v6.sin6_addr = ipv6_hdr(skb)->daddr;
  392. }
  393. }
  394. /* Initialize an sctp_addr from a socket. */
  395. static void sctp_v6_from_sk(union sctp_addr *addr, struct sock *sk)
  396. {
  397. addr->v6.sin6_family = AF_INET6;
  398. addr->v6.sin6_port = 0;
  399. addr->v6.sin6_addr = sk->sk_v6_rcv_saddr;
  400. }
  401. /* Initialize sk->sk_rcv_saddr from sctp_addr. */
  402. static void sctp_v6_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
  403. {
  404. if (addr->sa.sa_family == AF_INET) {
  405. sk->sk_v6_rcv_saddr.s6_addr32[0] = 0;
  406. sk->sk_v6_rcv_saddr.s6_addr32[1] = 0;
  407. sk->sk_v6_rcv_saddr.s6_addr32[2] = htonl(0x0000ffff);
  408. sk->sk_v6_rcv_saddr.s6_addr32[3] =
  409. addr->v4.sin_addr.s_addr;
  410. } else {
  411. sk->sk_v6_rcv_saddr = addr->v6.sin6_addr;
  412. }
  413. }
  414. /* Initialize sk->sk_daddr from sctp_addr. */
  415. static void sctp_v6_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
  416. {
  417. if (addr->sa.sa_family == AF_INET) {
  418. sk->sk_v6_daddr.s6_addr32[0] = 0;
  419. sk->sk_v6_daddr.s6_addr32[1] = 0;
  420. sk->sk_v6_daddr.s6_addr32[2] = htonl(0x0000ffff);
  421. sk->sk_v6_daddr.s6_addr32[3] = addr->v4.sin_addr.s_addr;
  422. } else {
  423. sk->sk_v6_daddr = addr->v6.sin6_addr;
  424. }
  425. }
  426. /* Initialize a sctp_addr from an address parameter. */
  427. static void sctp_v6_from_addr_param(union sctp_addr *addr,
  428. union sctp_addr_param *param,
  429. __be16 port, int iif)
  430. {
  431. addr->v6.sin6_family = AF_INET6;
  432. addr->v6.sin6_port = port;
  433. addr->v6.sin6_flowinfo = 0; /* BUG */
  434. addr->v6.sin6_addr = param->v6.addr;
  435. addr->v6.sin6_scope_id = iif;
  436. }
  437. /* Initialize an address parameter from a sctp_addr and return the length
  438. * of the address parameter.
  439. */
  440. static int sctp_v6_to_addr_param(const union sctp_addr *addr,
  441. union sctp_addr_param *param)
  442. {
  443. int length = sizeof(sctp_ipv6addr_param_t);
  444. param->v6.param_hdr.type = SCTP_PARAM_IPV6_ADDRESS;
  445. param->v6.param_hdr.length = htons(length);
  446. param->v6.addr = addr->v6.sin6_addr;
  447. return length;
  448. }
  449. /* Initialize a sctp_addr from struct in6_addr. */
  450. static void sctp_v6_to_addr(union sctp_addr *addr, struct in6_addr *saddr,
  451. __be16 port)
  452. {
  453. addr->sa.sa_family = AF_INET6;
  454. addr->v6.sin6_port = port;
  455. addr->v6.sin6_flowinfo = 0;
  456. addr->v6.sin6_addr = *saddr;
  457. addr->v6.sin6_scope_id = 0;
  458. }
  459. static int __sctp_v6_cmp_addr(const union sctp_addr *addr1,
  460. const union sctp_addr *addr2)
  461. {
  462. if (addr1->sa.sa_family != addr2->sa.sa_family) {
  463. if (addr1->sa.sa_family == AF_INET &&
  464. addr2->sa.sa_family == AF_INET6 &&
  465. ipv6_addr_v4mapped(&addr2->v6.sin6_addr) &&
  466. addr2->v6.sin6_addr.s6_addr32[3] ==
  467. addr1->v4.sin_addr.s_addr)
  468. return 1;
  469. if (addr2->sa.sa_family == AF_INET &&
  470. addr1->sa.sa_family == AF_INET6 &&
  471. ipv6_addr_v4mapped(&addr1->v6.sin6_addr) &&
  472. addr1->v6.sin6_addr.s6_addr32[3] ==
  473. addr2->v4.sin_addr.s_addr)
  474. return 1;
  475. return 0;
  476. }
  477. if (!ipv6_addr_equal(&addr1->v6.sin6_addr, &addr2->v6.sin6_addr))
  478. return 0;
  479. /* If this is a linklocal address, compare the scope_id. */
  480. if ((ipv6_addr_type(&addr1->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL) &&
  481. addr1->v6.sin6_scope_id && addr2->v6.sin6_scope_id &&
  482. addr1->v6.sin6_scope_id != addr2->v6.sin6_scope_id)
  483. return 0;
  484. return 1;
  485. }
  486. /* Compare addresses exactly.
  487. * v4-mapped-v6 is also in consideration.
  488. */
  489. static int sctp_v6_cmp_addr(const union sctp_addr *addr1,
  490. const union sctp_addr *addr2)
  491. {
  492. return __sctp_v6_cmp_addr(addr1, addr2) &&
  493. addr1->v6.sin6_port == addr2->v6.sin6_port;
  494. }
  495. /* Initialize addr struct to INADDR_ANY. */
  496. static void sctp_v6_inaddr_any(union sctp_addr *addr, __be16 port)
  497. {
  498. memset(addr, 0x00, sizeof(union sctp_addr));
  499. addr->v6.sin6_family = AF_INET6;
  500. addr->v6.sin6_port = port;
  501. }
  502. /* Is this a wildcard address? */
  503. static int sctp_v6_is_any(const union sctp_addr *addr)
  504. {
  505. return ipv6_addr_any(&addr->v6.sin6_addr);
  506. }
  507. /* Should this be available for binding? */
  508. static int sctp_v6_available(union sctp_addr *addr, struct sctp_sock *sp)
  509. {
  510. int type;
  511. struct net *net = sock_net(&sp->inet.sk);
  512. const struct in6_addr *in6 = (const struct in6_addr *)&addr->v6.sin6_addr;
  513. type = ipv6_addr_type(in6);
  514. if (IPV6_ADDR_ANY == type)
  515. return 1;
  516. if (type == IPV6_ADDR_MAPPED) {
  517. if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
  518. return 0;
  519. sctp_v6_map_v4(addr);
  520. return sctp_get_af_specific(AF_INET)->available(addr, sp);
  521. }
  522. if (!(type & IPV6_ADDR_UNICAST))
  523. return 0;
  524. return sp->inet.freebind || net->ipv6.sysctl.ip_nonlocal_bind ||
  525. ipv6_chk_addr(net, in6, NULL, 0);
  526. }
  527. /* This function checks if the address is a valid address to be used for
  528. * SCTP.
  529. *
  530. * Output:
  531. * Return 0 - If the address is a non-unicast or an illegal address.
  532. * Return 1 - If the address is a unicast.
  533. */
  534. static int sctp_v6_addr_valid(union sctp_addr *addr,
  535. struct sctp_sock *sp,
  536. const struct sk_buff *skb)
  537. {
  538. int ret = ipv6_addr_type(&addr->v6.sin6_addr);
  539. /* Support v4-mapped-v6 address. */
  540. if (ret == IPV6_ADDR_MAPPED) {
  541. /* Note: This routine is used in input, so v4-mapped-v6
  542. * are disallowed here when there is no sctp_sock.
  543. */
  544. if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
  545. return 0;
  546. sctp_v6_map_v4(addr);
  547. return sctp_get_af_specific(AF_INET)->addr_valid(addr, sp, skb);
  548. }
  549. /* Is this a non-unicast address */
  550. if (!(ret & IPV6_ADDR_UNICAST))
  551. return 0;
  552. return 1;
  553. }
  554. /* What is the scope of 'addr'? */
  555. static sctp_scope_t sctp_v6_scope(union sctp_addr *addr)
  556. {
  557. int v6scope;
  558. sctp_scope_t retval;
  559. /* The IPv6 scope is really a set of bit fields.
  560. * See IFA_* in <net/if_inet6.h>. Map to a generic SCTP scope.
  561. */
  562. v6scope = ipv6_addr_scope(&addr->v6.sin6_addr);
  563. switch (v6scope) {
  564. case IFA_HOST:
  565. retval = SCTP_SCOPE_LOOPBACK;
  566. break;
  567. case IFA_LINK:
  568. retval = SCTP_SCOPE_LINK;
  569. break;
  570. case IFA_SITE:
  571. retval = SCTP_SCOPE_PRIVATE;
  572. break;
  573. default:
  574. retval = SCTP_SCOPE_GLOBAL;
  575. break;
  576. }
  577. return retval;
  578. }
  579. /* Create and initialize a new sk for the socket to be returned by accept(). */
  580. static struct sock *sctp_v6_create_accept_sk(struct sock *sk,
  581. struct sctp_association *asoc)
  582. {
  583. struct sock *newsk;
  584. struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
  585. struct sctp6_sock *newsctp6sk;
  586. struct ipv6_txoptions *opt;
  587. newsk = sk_alloc(sock_net(sk), PF_INET6, GFP_KERNEL, sk->sk_prot, 0);
  588. if (!newsk)
  589. goto out;
  590. sock_init_data(NULL, newsk);
  591. sctp_copy_sock(newsk, sk, asoc);
  592. sock_reset_flag(sk, SOCK_ZAPPED);
  593. newsctp6sk = (struct sctp6_sock *)newsk;
  594. inet_sk(newsk)->pinet6 = &newsctp6sk->inet6;
  595. sctp_sk(newsk)->v4mapped = sctp_sk(sk)->v4mapped;
  596. newnp = inet6_sk(newsk);
  597. memcpy(newnp, np, sizeof(struct ipv6_pinfo));
  598. newnp->ipv6_mc_list = NULL;
  599. newnp->ipv6_ac_list = NULL;
  600. newnp->ipv6_fl_list = NULL;
  601. rcu_read_lock();
  602. opt = rcu_dereference(np->opt);
  603. if (opt)
  604. opt = ipv6_dup_options(newsk, opt);
  605. RCU_INIT_POINTER(newnp->opt, opt);
  606. rcu_read_unlock();
  607. /* Initialize sk's sport, dport, rcv_saddr and daddr for getsockname()
  608. * and getpeername().
  609. */
  610. sctp_v6_to_sk_daddr(&asoc->peer.primary_addr, newsk);
  611. newsk->sk_v6_rcv_saddr = sk->sk_v6_rcv_saddr;
  612. sk_refcnt_debug_inc(newsk);
  613. if (newsk->sk_prot->init(newsk)) {
  614. sk_common_release(newsk);
  615. newsk = NULL;
  616. }
  617. out:
  618. return newsk;
  619. }
  620. /* Format a sockaddr for return to user space. This makes sure the return is
  621. * AF_INET or AF_INET6 depending on the SCTP_I_WANT_MAPPED_V4_ADDR option.
  622. */
  623. static int sctp_v6_addr_to_user(struct sctp_sock *sp, union sctp_addr *addr)
  624. {
  625. if (sp->v4mapped) {
  626. if (addr->sa.sa_family == AF_INET)
  627. sctp_v4_map_v6(addr);
  628. } else {
  629. if (addr->sa.sa_family == AF_INET6 &&
  630. ipv6_addr_v4mapped(&addr->v6.sin6_addr))
  631. sctp_v6_map_v4(addr);
  632. }
  633. if (addr->sa.sa_family == AF_INET) {
  634. memset(addr->v4.sin_zero, 0, sizeof(addr->v4.sin_zero));
  635. return sizeof(struct sockaddr_in);
  636. }
  637. return sizeof(struct sockaddr_in6);
  638. }
  639. /* Where did this skb come from? */
  640. static int sctp_v6_skb_iif(const struct sk_buff *skb)
  641. {
  642. return IP6CB(skb)->iif;
  643. }
  644. /* Was this packet marked by Explicit Congestion Notification? */
  645. static int sctp_v6_is_ce(const struct sk_buff *skb)
  646. {
  647. return *((__u32 *)(ipv6_hdr(skb))) & htonl(1 << 20);
  648. }
  649. /* Dump the v6 addr to the seq file. */
  650. static void sctp_v6_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
  651. {
  652. seq_printf(seq, "%pI6 ", &addr->v6.sin6_addr);
  653. }
  654. static void sctp_v6_ecn_capable(struct sock *sk)
  655. {
  656. inet6_sk(sk)->tclass |= INET_ECN_ECT_0;
  657. }
  658. /* Initialize a PF_INET msgname from a ulpevent. */
  659. static void sctp_inet6_event_msgname(struct sctp_ulpevent *event,
  660. char *msgname, int *addrlen)
  661. {
  662. union sctp_addr *addr;
  663. struct sctp_association *asoc;
  664. union sctp_addr *paddr;
  665. if (!msgname)
  666. return;
  667. addr = (union sctp_addr *)msgname;
  668. asoc = event->asoc;
  669. paddr = &asoc->peer.primary_addr;
  670. if (paddr->sa.sa_family == AF_INET) {
  671. addr->v4.sin_family = AF_INET;
  672. addr->v4.sin_port = htons(asoc->peer.port);
  673. addr->v4.sin_addr = paddr->v4.sin_addr;
  674. } else {
  675. addr->v6.sin6_family = AF_INET6;
  676. addr->v6.sin6_flowinfo = 0;
  677. if (ipv6_addr_type(&paddr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
  678. addr->v6.sin6_scope_id = paddr->v6.sin6_scope_id;
  679. else
  680. addr->v6.sin6_scope_id = 0;
  681. addr->v6.sin6_port = htons(asoc->peer.port);
  682. addr->v6.sin6_addr = paddr->v6.sin6_addr;
  683. }
  684. *addrlen = sctp_v6_addr_to_user(sctp_sk(asoc->base.sk), addr);
  685. }
  686. /* Initialize a msg_name from an inbound skb. */
  687. static void sctp_inet6_skb_msgname(struct sk_buff *skb, char *msgname,
  688. int *addr_len)
  689. {
  690. union sctp_addr *addr;
  691. struct sctphdr *sh;
  692. if (!msgname)
  693. return;
  694. addr = (union sctp_addr *)msgname;
  695. sh = sctp_hdr(skb);
  696. if (ip_hdr(skb)->version == 4) {
  697. addr->v4.sin_family = AF_INET;
  698. addr->v4.sin_port = sh->source;
  699. addr->v4.sin_addr.s_addr = ip_hdr(skb)->saddr;
  700. } else {
  701. addr->v6.sin6_family = AF_INET6;
  702. addr->v6.sin6_flowinfo = 0;
  703. addr->v6.sin6_port = sh->source;
  704. addr->v6.sin6_addr = ipv6_hdr(skb)->saddr;
  705. if (ipv6_addr_type(&addr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
  706. addr->v6.sin6_scope_id = sctp_v6_skb_iif(skb);
  707. else
  708. addr->v6.sin6_scope_id = 0;
  709. }
  710. *addr_len = sctp_v6_addr_to_user(sctp_sk(skb->sk), addr);
  711. }
  712. /* Do we support this AF? */
  713. static int sctp_inet6_af_supported(sa_family_t family, struct sctp_sock *sp)
  714. {
  715. switch (family) {
  716. case AF_INET6:
  717. return 1;
  718. /* v4-mapped-v6 addresses */
  719. case AF_INET:
  720. if (!__ipv6_only_sock(sctp_opt2sk(sp)))
  721. return 1;
  722. default:
  723. return 0;
  724. }
  725. }
  726. /* Address matching with wildcards allowed. This extra level
  727. * of indirection lets us choose whether a PF_INET6 should
  728. * disallow any v4 addresses if we so choose.
  729. */
  730. static int sctp_inet6_cmp_addr(const union sctp_addr *addr1,
  731. const union sctp_addr *addr2,
  732. struct sctp_sock *opt)
  733. {
  734. struct sock *sk = sctp_opt2sk(opt);
  735. struct sctp_af *af1, *af2;
  736. af1 = sctp_get_af_specific(addr1->sa.sa_family);
  737. af2 = sctp_get_af_specific(addr2->sa.sa_family);
  738. if (!af1 || !af2)
  739. return 0;
  740. /* If the socket is IPv6 only, v4 addrs will not match */
  741. if (__ipv6_only_sock(sk) && af1 != af2)
  742. return 0;
  743. /* Today, wildcard AF_INET/AF_INET6. */
  744. if (sctp_is_any(sk, addr1) || sctp_is_any(sk, addr2))
  745. return 1;
  746. if (addr1->sa.sa_family == AF_INET && addr2->sa.sa_family == AF_INET)
  747. return addr1->v4.sin_addr.s_addr == addr2->v4.sin_addr.s_addr;
  748. return __sctp_v6_cmp_addr(addr1, addr2);
  749. }
  750. /* Verify that the provided sockaddr looks bindable. Common verification,
  751. * has already been taken care of.
  752. */
  753. static int sctp_inet6_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
  754. {
  755. struct sctp_af *af;
  756. /* ASSERT: address family has already been verified. */
  757. if (addr->sa.sa_family != AF_INET6)
  758. af = sctp_get_af_specific(addr->sa.sa_family);
  759. else {
  760. int type = ipv6_addr_type(&addr->v6.sin6_addr);
  761. struct net_device *dev;
  762. if (type & IPV6_ADDR_LINKLOCAL) {
  763. struct net *net;
  764. if (!addr->v6.sin6_scope_id)
  765. return 0;
  766. net = sock_net(&opt->inet.sk);
  767. rcu_read_lock();
  768. dev = dev_get_by_index_rcu(net, addr->v6.sin6_scope_id);
  769. if (!dev || !(opt->inet.freebind ||
  770. net->ipv6.sysctl.ip_nonlocal_bind ||
  771. ipv6_chk_addr(net, &addr->v6.sin6_addr,
  772. dev, 0))) {
  773. rcu_read_unlock();
  774. return 0;
  775. }
  776. rcu_read_unlock();
  777. }
  778. af = opt->pf->af;
  779. }
  780. return af->available(addr, opt);
  781. }
  782. /* Verify that the provided sockaddr looks sendable. Common verification,
  783. * has already been taken care of.
  784. */
  785. static int sctp_inet6_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
  786. {
  787. struct sctp_af *af = NULL;
  788. /* ASSERT: address family has already been verified. */
  789. if (addr->sa.sa_family != AF_INET6)
  790. af = sctp_get_af_specific(addr->sa.sa_family);
  791. else {
  792. int type = ipv6_addr_type(&addr->v6.sin6_addr);
  793. struct net_device *dev;
  794. if (type & IPV6_ADDR_LINKLOCAL) {
  795. if (!addr->v6.sin6_scope_id)
  796. return 0;
  797. rcu_read_lock();
  798. dev = dev_get_by_index_rcu(sock_net(&opt->inet.sk),
  799. addr->v6.sin6_scope_id);
  800. rcu_read_unlock();
  801. if (!dev)
  802. return 0;
  803. }
  804. af = opt->pf->af;
  805. }
  806. return af != NULL;
  807. }
  808. /* Fill in Supported Address Type information for INIT and INIT-ACK
  809. * chunks. Note: In the future, we may want to look at sock options
  810. * to determine whether a PF_INET6 socket really wants to have IPV4
  811. * addresses.
  812. * Returns number of addresses supported.
  813. */
  814. static int sctp_inet6_supported_addrs(const struct sctp_sock *opt,
  815. __be16 *types)
  816. {
  817. types[0] = SCTP_PARAM_IPV6_ADDRESS;
  818. if (!opt || !ipv6_only_sock(sctp_opt2sk(opt))) {
  819. types[1] = SCTP_PARAM_IPV4_ADDRESS;
  820. return 2;
  821. }
  822. return 1;
  823. }
  824. /* Handle SCTP_I_WANT_MAPPED_V4_ADDR for getpeername() and getsockname() */
  825. static int sctp_getname(struct socket *sock, struct sockaddr *uaddr,
  826. int *uaddr_len, int peer)
  827. {
  828. int rc;
  829. rc = inet6_getname(sock, uaddr, uaddr_len, peer);
  830. if (rc != 0)
  831. return rc;
  832. *uaddr_len = sctp_v6_addr_to_user(sctp_sk(sock->sk),
  833. (union sctp_addr *)uaddr);
  834. return rc;
  835. }
  836. static const struct proto_ops inet6_seqpacket_ops = {
  837. .family = PF_INET6,
  838. .owner = THIS_MODULE,
  839. .release = inet6_release,
  840. .bind = inet6_bind,
  841. .connect = sctp_inet_connect,
  842. .socketpair = sock_no_socketpair,
  843. .accept = inet_accept,
  844. .getname = sctp_getname,
  845. .poll = sctp_poll,
  846. .ioctl = inet6_ioctl,
  847. .listen = sctp_inet_listen,
  848. .shutdown = inet_shutdown,
  849. .setsockopt = sock_common_setsockopt,
  850. .getsockopt = sock_common_getsockopt,
  851. .sendmsg = inet_sendmsg,
  852. .recvmsg = inet_recvmsg,
  853. .mmap = sock_no_mmap,
  854. #ifdef CONFIG_COMPAT
  855. .compat_setsockopt = compat_sock_common_setsockopt,
  856. .compat_getsockopt = compat_sock_common_getsockopt,
  857. #endif
  858. };
  859. static struct inet_protosw sctpv6_seqpacket_protosw = {
  860. .type = SOCK_SEQPACKET,
  861. .protocol = IPPROTO_SCTP,
  862. .prot = &sctpv6_prot,
  863. .ops = &inet6_seqpacket_ops,
  864. .flags = SCTP_PROTOSW_FLAG
  865. };
  866. static struct inet_protosw sctpv6_stream_protosw = {
  867. .type = SOCK_STREAM,
  868. .protocol = IPPROTO_SCTP,
  869. .prot = &sctpv6_prot,
  870. .ops = &inet6_seqpacket_ops,
  871. .flags = SCTP_PROTOSW_FLAG,
  872. };
  873. static int sctp6_rcv(struct sk_buff *skb)
  874. {
  875. return sctp_rcv(skb) ? -1 : 0;
  876. }
  877. static const struct inet6_protocol sctpv6_protocol = {
  878. .handler = sctp6_rcv,
  879. .err_handler = sctp_v6_err,
  880. .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL,
  881. };
  882. static struct sctp_af sctp_af_inet6 = {
  883. .sa_family = AF_INET6,
  884. .sctp_xmit = sctp_v6_xmit,
  885. .setsockopt = ipv6_setsockopt,
  886. .getsockopt = ipv6_getsockopt,
  887. .get_dst = sctp_v6_get_dst,
  888. .get_saddr = sctp_v6_get_saddr,
  889. .copy_addrlist = sctp_v6_copy_addrlist,
  890. .from_skb = sctp_v6_from_skb,
  891. .from_sk = sctp_v6_from_sk,
  892. .from_addr_param = sctp_v6_from_addr_param,
  893. .to_addr_param = sctp_v6_to_addr_param,
  894. .cmp_addr = sctp_v6_cmp_addr,
  895. .scope = sctp_v6_scope,
  896. .addr_valid = sctp_v6_addr_valid,
  897. .inaddr_any = sctp_v6_inaddr_any,
  898. .is_any = sctp_v6_is_any,
  899. .available = sctp_v6_available,
  900. .skb_iif = sctp_v6_skb_iif,
  901. .is_ce = sctp_v6_is_ce,
  902. .seq_dump_addr = sctp_v6_seq_dump_addr,
  903. .ecn_capable = sctp_v6_ecn_capable,
  904. .net_header_len = sizeof(struct ipv6hdr),
  905. .sockaddr_len = sizeof(struct sockaddr_in6),
  906. #ifdef CONFIG_COMPAT
  907. .compat_setsockopt = compat_ipv6_setsockopt,
  908. .compat_getsockopt = compat_ipv6_getsockopt,
  909. #endif
  910. };
  911. static struct sctp_pf sctp_pf_inet6 = {
  912. .event_msgname = sctp_inet6_event_msgname,
  913. .skb_msgname = sctp_inet6_skb_msgname,
  914. .af_supported = sctp_inet6_af_supported,
  915. .cmp_addr = sctp_inet6_cmp_addr,
  916. .bind_verify = sctp_inet6_bind_verify,
  917. .send_verify = sctp_inet6_send_verify,
  918. .supported_addrs = sctp_inet6_supported_addrs,
  919. .create_accept_sk = sctp_v6_create_accept_sk,
  920. .addr_to_user = sctp_v6_addr_to_user,
  921. .to_sk_saddr = sctp_v6_to_sk_saddr,
  922. .to_sk_daddr = sctp_v6_to_sk_daddr,
  923. .af = &sctp_af_inet6,
  924. };
  925. /* Initialize IPv6 support and register with socket layer. */
  926. void sctp_v6_pf_init(void)
  927. {
  928. /* Register the SCTP specific PF_INET6 functions. */
  929. sctp_register_pf(&sctp_pf_inet6, PF_INET6);
  930. /* Register the SCTP specific AF_INET6 functions. */
  931. sctp_register_af(&sctp_af_inet6);
  932. }
  933. void sctp_v6_pf_exit(void)
  934. {
  935. list_del(&sctp_af_inet6.list);
  936. }
  937. /* Initialize IPv6 support and register with socket layer. */
  938. int sctp_v6_protosw_init(void)
  939. {
  940. int rc;
  941. rc = proto_register(&sctpv6_prot, 1);
  942. if (rc)
  943. return rc;
  944. /* Add SCTPv6(UDP and TCP style) to inetsw6 linked list. */
  945. inet6_register_protosw(&sctpv6_seqpacket_protosw);
  946. inet6_register_protosw(&sctpv6_stream_protosw);
  947. return 0;
  948. }
  949. void sctp_v6_protosw_exit(void)
  950. {
  951. inet6_unregister_protosw(&sctpv6_seqpacket_protosw);
  952. inet6_unregister_protosw(&sctpv6_stream_protosw);
  953. proto_unregister(&sctpv6_prot);
  954. }
  955. /* Register with inet6 layer. */
  956. int sctp_v6_add_protocol(void)
  957. {
  958. /* Register notifier for inet6 address additions/deletions. */
  959. register_inet6addr_notifier(&sctp_inet6addr_notifier);
  960. if (inet6_add_protocol(&sctpv6_protocol, IPPROTO_SCTP) < 0)
  961. return -EAGAIN;
  962. return 0;
  963. }
  964. /* Unregister with inet6 layer. */
  965. void sctp_v6_del_protocol(void)
  966. {
  967. inet6_del_protocol(&sctpv6_protocol, IPPROTO_SCTP);
  968. unregister_inet6addr_notifier(&sctp_inet6addr_notifier);
  969. }