ipv6.c 28 KB

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