Thomas Gleixner | 1ccea77 | 2019-05-19 15:51:43 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Noriaki TAKAMIYA | 2c8d7ca | 2006-08-23 20:31:11 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C)2003-2006 Helsinki University of Technology |
| 4 | * Copyright (C)2003-2006 USAGI/WIDE Project |
Noriaki TAKAMIYA | 2c8d7ca | 2006-08-23 20:31:11 -0700 | [diff] [blame] | 5 | */ |
| 6 | /* |
| 7 | * Authors: |
| 8 | * Noriaki TAKAMIYA @USAGI |
| 9 | * Masahide NAKAMURA @USAGI |
| 10 | */ |
| 11 | |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 13 | |
Noriaki TAKAMIYA | 2c8d7ca | 2006-08-23 20:31:11 -0700 | [diff] [blame] | 14 | #include <linux/module.h> |
| 15 | #include <linux/skbuff.h> |
Masahide NAKAMURA | 70182ed | 2006-08-23 20:45:55 -0700 | [diff] [blame] | 16 | #include <linux/time.h> |
Noriaki TAKAMIYA | 2c8d7ca | 2006-08-23 20:31:11 -0700 | [diff] [blame] | 17 | #include <linux/ipv6.h> |
Masahide NAKAMURA | 7be96f7 | 2006-08-23 20:35:31 -0700 | [diff] [blame] | 18 | #include <linux/icmpv6.h> |
| 19 | #include <net/sock.h> |
Noriaki TAKAMIYA | 2c8d7ca | 2006-08-23 20:31:11 -0700 | [diff] [blame] | 20 | #include <net/ipv6.h> |
Masahide NAKAMURA | 7be96f7 | 2006-08-23 20:35:31 -0700 | [diff] [blame] | 21 | #include <net/ip6_checksum.h> |
Masahide NAKAMURA | 59fbb3a6 | 2007-06-26 23:56:32 -0700 | [diff] [blame] | 22 | #include <net/rawv6.h> |
Noriaki TAKAMIYA | 2c8d7ca | 2006-08-23 20:31:11 -0700 | [diff] [blame] | 23 | #include <net/xfrm.h> |
| 24 | #include <net/mip6.h> |
| 25 | |
Noriaki TAKAMIYA | 3d12689 | 2006-08-23 20:32:34 -0700 | [diff] [blame] | 26 | static inline unsigned int calc_padlen(unsigned int len, unsigned int n) |
| 27 | { |
| 28 | return (n - len + 16) & 0x7; |
| 29 | } |
| 30 | |
| 31 | static inline void *mip6_padn(__u8 *data, __u8 padlen) |
| 32 | { |
| 33 | if (!data) |
| 34 | return NULL; |
| 35 | if (padlen == 1) { |
Eldad Zack | 1de5a71 | 2012-05-17 06:00:25 +0000 | [diff] [blame] | 36 | data[0] = IPV6_TLV_PAD1; |
Noriaki TAKAMIYA | 3d12689 | 2006-08-23 20:32:34 -0700 | [diff] [blame] | 37 | } else if (padlen > 1) { |
YOSHIFUJI Hideaki | 7f1eced | 2008-04-10 15:42:12 +0900 | [diff] [blame] | 38 | data[0] = IPV6_TLV_PADN; |
Noriaki TAKAMIYA | 3d12689 | 2006-08-23 20:32:34 -0700 | [diff] [blame] | 39 | data[1] = padlen - 2; |
| 40 | if (padlen > 2) |
| 41 | memset(data+2, 0, data[1]); |
| 42 | } |
| 43 | return data + padlen; |
| 44 | } |
| 45 | |
Brian Haley | d5fdd6b | 2009-06-23 04:31:07 -0700 | [diff] [blame] | 46 | static inline void mip6_param_prob(struct sk_buff *skb, u8 code, int pos) |
Masahide NAKAMURA | 7be96f7 | 2006-08-23 20:35:31 -0700 | [diff] [blame] | 47 | { |
Alexey Dobriyan | 3ffe533 | 2010-02-18 08:25:24 +0000 | [diff] [blame] | 48 | icmpv6_send(skb, ICMPV6_PARAMPROB, code, pos); |
Masahide NAKAMURA | 7be96f7 | 2006-08-23 20:35:31 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | static int mip6_mh_len(int type) |
| 52 | { |
| 53 | int len = 0; |
| 54 | |
| 55 | switch (type) { |
| 56 | case IP6_MH_TYPE_BRR: |
| 57 | len = 0; |
| 58 | break; |
| 59 | case IP6_MH_TYPE_HOTI: |
| 60 | case IP6_MH_TYPE_COTI: |
| 61 | case IP6_MH_TYPE_BU: |
| 62 | case IP6_MH_TYPE_BACK: |
| 63 | len = 1; |
| 64 | break; |
| 65 | case IP6_MH_TYPE_HOT: |
| 66 | case IP6_MH_TYPE_COT: |
| 67 | case IP6_MH_TYPE_BERROR: |
| 68 | len = 2; |
| 69 | break; |
| 70 | } |
| 71 | return len; |
| 72 | } |
| 73 | |
Masahide NAKAMURA | 59fbb3a6 | 2007-06-26 23:56:32 -0700 | [diff] [blame] | 74 | static int mip6_mh_filter(struct sock *sk, struct sk_buff *skb) |
Masahide NAKAMURA | 7be96f7 | 2006-08-23 20:35:31 -0700 | [diff] [blame] | 75 | { |
Eric Dumazet | 96af69e | 2012-09-25 22:01:28 +0200 | [diff] [blame] | 76 | struct ip6_mh _hdr; |
| 77 | const struct ip6_mh *mh; |
Masahide NAKAMURA | 7be96f7 | 2006-08-23 20:35:31 -0700 | [diff] [blame] | 78 | |
Eric Dumazet | 96af69e | 2012-09-25 22:01:28 +0200 | [diff] [blame] | 79 | mh = skb_header_pointer(skb, skb_transport_offset(skb), |
| 80 | sizeof(_hdr), &_hdr); |
| 81 | if (!mh) |
Masahide NAKAMURA | 7be96f7 | 2006-08-23 20:35:31 -0700 | [diff] [blame] | 82 | return -1; |
| 83 | |
Eric Dumazet | 96af69e | 2012-09-25 22:01:28 +0200 | [diff] [blame] | 84 | if (((mh->ip6mh_hdrlen + 1) << 3) > skb->len) |
| 85 | return -1; |
Masahide NAKAMURA | 7be96f7 | 2006-08-23 20:35:31 -0700 | [diff] [blame] | 86 | |
| 87 | if (mh->ip6mh_hdrlen < mip6_mh_len(mh->ip6mh_type)) { |
Joe Perches | ba7a46f | 2014-11-11 10:59:17 -0800 | [diff] [blame] | 88 | net_dbg_ratelimited("mip6: MH message too short: %d vs >=%d\n", |
| 89 | mh->ip6mh_hdrlen, |
| 90 | mip6_mh_len(mh->ip6mh_type)); |
Eric Dumazet | 96af69e | 2012-09-25 22:01:28 +0200 | [diff] [blame] | 91 | mip6_param_prob(skb, 0, offsetof(struct ip6_mh, ip6mh_hdrlen) + |
| 92 | skb_network_header_len(skb)); |
Masahide NAKAMURA | 7be96f7 | 2006-08-23 20:35:31 -0700 | [diff] [blame] | 93 | return -1; |
| 94 | } |
Masahide NAKAMURA | 7be96f7 | 2006-08-23 20:35:31 -0700 | [diff] [blame] | 95 | |
| 96 | if (mh->ip6mh_proto != IPPROTO_NONE) { |
Joe Perches | ba7a46f | 2014-11-11 10:59:17 -0800 | [diff] [blame] | 97 | net_dbg_ratelimited("mip6: MH invalid payload proto = %d\n", |
| 98 | mh->ip6mh_proto); |
Eric Dumazet | 96af69e | 2012-09-25 22:01:28 +0200 | [diff] [blame] | 99 | mip6_param_prob(skb, 0, offsetof(struct ip6_mh, ip6mh_proto) + |
| 100 | skb_network_header_len(skb)); |
Masahide NAKAMURA | 7be96f7 | 2006-08-23 20:35:31 -0700 | [diff] [blame] | 101 | return -1; |
| 102 | } |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
Masahide NAKAMURA | 70182ed | 2006-08-23 20:45:55 -0700 | [diff] [blame] | 107 | struct mip6_report_rate_limiter { |
| 108 | spinlock_t lock; |
Arnd Bergmann | 3dd7669 | 2015-09-30 13:26:39 +0200 | [diff] [blame] | 109 | ktime_t stamp; |
Masahide NAKAMURA | 70182ed | 2006-08-23 20:45:55 -0700 | [diff] [blame] | 110 | int iif; |
| 111 | struct in6_addr src; |
| 112 | struct in6_addr dst; |
| 113 | }; |
| 114 | |
| 115 | static struct mip6_report_rate_limiter mip6_report_rl = { |
Milind Arun Choudhary | 4ef8d0a | 2007-04-26 01:37:44 -0700 | [diff] [blame] | 116 | .lock = __SPIN_LOCK_UNLOCKED(mip6_report_rl.lock) |
Masahide NAKAMURA | 70182ed | 2006-08-23 20:45:55 -0700 | [diff] [blame] | 117 | }; |
| 118 | |
Noriaki TAKAMIYA | 3d12689 | 2006-08-23 20:32:34 -0700 | [diff] [blame] | 119 | static int mip6_destopt_input(struct xfrm_state *x, struct sk_buff *skb) |
| 120 | { |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 121 | const struct ipv6hdr *iph = ipv6_hdr(skb); |
Noriaki TAKAMIYA | 3d12689 | 2006-08-23 20:32:34 -0700 | [diff] [blame] | 122 | struct ipv6_destopt_hdr *destopt = (struct ipv6_destopt_hdr *)skb->data; |
Herbert Xu | 0ebea8e | 2007-11-13 21:45:58 -0800 | [diff] [blame] | 123 | int err = destopt->nexthdr; |
Noriaki TAKAMIYA | 3d12689 | 2006-08-23 20:32:34 -0700 | [diff] [blame] | 124 | |
Herbert Xu | 0ebea8e | 2007-11-13 21:45:58 -0800 | [diff] [blame] | 125 | spin_lock(&x->lock); |
Noriaki TAKAMIYA | 3d12689 | 2006-08-23 20:32:34 -0700 | [diff] [blame] | 126 | if (!ipv6_addr_equal(&iph->saddr, (struct in6_addr *)x->coaddr) && |
| 127 | !ipv6_addr_any((struct in6_addr *)x->coaddr)) |
Herbert Xu | 0ebea8e | 2007-11-13 21:45:58 -0800 | [diff] [blame] | 128 | err = -ENOENT; |
| 129 | spin_unlock(&x->lock); |
Noriaki TAKAMIYA | 3d12689 | 2006-08-23 20:32:34 -0700 | [diff] [blame] | 130 | |
Herbert Xu | 0ebea8e | 2007-11-13 21:45:58 -0800 | [diff] [blame] | 131 | return err; |
Noriaki TAKAMIYA | 3d12689 | 2006-08-23 20:32:34 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /* Destination Option Header is inserted. |
| 135 | * IP Header's src address is replaced with Home Address Option in |
| 136 | * Destination Option Header. |
| 137 | */ |
| 138 | static int mip6_destopt_output(struct xfrm_state *x, struct sk_buff *skb) |
| 139 | { |
| 140 | struct ipv6hdr *iph; |
| 141 | struct ipv6_destopt_hdr *dstopt; |
| 142 | struct ipv6_destopt_hao *hao; |
| 143 | u8 nexthdr; |
| 144 | int len; |
| 145 | |
Herbert Xu | 7b277b1 | 2007-10-10 15:44:06 -0700 | [diff] [blame] | 146 | skb_push(skb, -skb_network_offset(skb)); |
Herbert Xu | 007f021 | 2007-10-09 13:25:59 -0700 | [diff] [blame] | 147 | iph = ipv6_hdr(skb); |
Noriaki TAKAMIYA | 3d12689 | 2006-08-23 20:32:34 -0700 | [diff] [blame] | 148 | |
Herbert Xu | 007f021 | 2007-10-09 13:25:59 -0700 | [diff] [blame] | 149 | nexthdr = *skb_mac_header(skb); |
| 150 | *skb_mac_header(skb) = IPPROTO_DSTOPTS; |
Noriaki TAKAMIYA | 3d12689 | 2006-08-23 20:32:34 -0700 | [diff] [blame] | 151 | |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 152 | dstopt = (struct ipv6_destopt_hdr *)skb_transport_header(skb); |
Noriaki TAKAMIYA | 3d12689 | 2006-08-23 20:32:34 -0700 | [diff] [blame] | 153 | dstopt->nexthdr = nexthdr; |
| 154 | |
| 155 | hao = mip6_padn((char *)(dstopt + 1), |
| 156 | calc_padlen(sizeof(*dstopt), 6)); |
| 157 | |
| 158 | hao->type = IPV6_TLV_HAO; |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 159 | BUILD_BUG_ON(sizeof(*hao) != 18); |
Noriaki TAKAMIYA | 3d12689 | 2006-08-23 20:32:34 -0700 | [diff] [blame] | 160 | hao->length = sizeof(*hao) - 2; |
Noriaki TAKAMIYA | 3d12689 | 2006-08-23 20:32:34 -0700 | [diff] [blame] | 161 | |
| 162 | len = ((char *)hao - (char *)dstopt) + sizeof(*hao); |
| 163 | |
| 164 | memcpy(&hao->addr, &iph->saddr, sizeof(hao->addr)); |
Herbert Xu | b7c6538c | 2007-10-09 13:33:35 -0700 | [diff] [blame] | 165 | spin_lock_bh(&x->lock); |
Noriaki TAKAMIYA | 3d12689 | 2006-08-23 20:32:34 -0700 | [diff] [blame] | 166 | memcpy(&iph->saddr, x->coaddr, sizeof(iph->saddr)); |
Herbert Xu | b7c6538c | 2007-10-09 13:33:35 -0700 | [diff] [blame] | 167 | spin_unlock_bh(&x->lock); |
Noriaki TAKAMIYA | 3d12689 | 2006-08-23 20:32:34 -0700 | [diff] [blame] | 168 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 169 | WARN_ON(len != x->props.header_len); |
Noriaki TAKAMIYA | 3d12689 | 2006-08-23 20:32:34 -0700 | [diff] [blame] | 170 | dstopt->hdrlen = (x->props.header_len >> 3) - 1; |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
Arnd Bergmann | 3dd7669 | 2015-09-30 13:26:39 +0200 | [diff] [blame] | 175 | static inline int mip6_report_rl_allow(ktime_t stamp, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 176 | const struct in6_addr *dst, |
| 177 | const struct in6_addr *src, int iif) |
Masahide NAKAMURA | 70182ed | 2006-08-23 20:45:55 -0700 | [diff] [blame] | 178 | { |
| 179 | int allow = 0; |
| 180 | |
| 181 | spin_lock_bh(&mip6_report_rl.lock); |
Thomas Gleixner | 1f3a8e4 | 2016-12-25 12:43:07 +0100 | [diff] [blame] | 182 | if (mip6_report_rl.stamp != stamp || |
Masahide NAKAMURA | 70182ed | 2006-08-23 20:45:55 -0700 | [diff] [blame] | 183 | mip6_report_rl.iif != iif || |
| 184 | !ipv6_addr_equal(&mip6_report_rl.src, src) || |
| 185 | !ipv6_addr_equal(&mip6_report_rl.dst, dst)) { |
Arnd Bergmann | 3dd7669 | 2015-09-30 13:26:39 +0200 | [diff] [blame] | 186 | mip6_report_rl.stamp = stamp; |
Masahide NAKAMURA | 70182ed | 2006-08-23 20:45:55 -0700 | [diff] [blame] | 187 | mip6_report_rl.iif = iif; |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 188 | mip6_report_rl.src = *src; |
| 189 | mip6_report_rl.dst = *dst; |
Masahide NAKAMURA | 70182ed | 2006-08-23 20:45:55 -0700 | [diff] [blame] | 190 | allow = 1; |
| 191 | } |
| 192 | spin_unlock_bh(&mip6_report_rl.lock); |
| 193 | return allow; |
| 194 | } |
| 195 | |
David S. Miller | 8f029de | 2011-02-22 17:59:59 -0800 | [diff] [blame] | 196 | static int mip6_destopt_reject(struct xfrm_state *x, struct sk_buff *skb, |
| 197 | const struct flowi *fl) |
Masahide NAKAMURA | 70182ed | 2006-08-23 20:45:55 -0700 | [diff] [blame] | 198 | { |
Alexey Dobriyan | db983c1 | 2008-11-25 17:51:01 -0800 | [diff] [blame] | 199 | struct net *net = xs_net(x); |
Masahide NAKAMURA | 70182ed | 2006-08-23 20:45:55 -0700 | [diff] [blame] | 200 | struct inet6_skb_parm *opt = (struct inet6_skb_parm *)skb->cb; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 201 | const struct flowi6 *fl6 = &fl->u.ip6; |
Masahide NAKAMURA | 70182ed | 2006-08-23 20:45:55 -0700 | [diff] [blame] | 202 | struct ipv6_destopt_hao *hao = NULL; |
| 203 | struct xfrm_selector sel; |
| 204 | int offset; |
Arnd Bergmann | 3dd7669 | 2015-09-30 13:26:39 +0200 | [diff] [blame] | 205 | ktime_t stamp; |
Masahide NAKAMURA | 70182ed | 2006-08-23 20:45:55 -0700 | [diff] [blame] | 206 | int err = 0; |
| 207 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 208 | if (unlikely(fl6->flowi6_proto == IPPROTO_MH && |
David S. Miller | 1958b85 | 2011-03-12 16:36:19 -0500 | [diff] [blame] | 209 | fl6->fl6_mh_type <= IP6_MH_TYPE_MAX)) |
Masahide NAKAMURA | 01be8e5 | 2006-08-23 20:47:44 -0700 | [diff] [blame] | 210 | goto out; |
| 211 | |
Masahide NAKAMURA | 70182ed | 2006-08-23 20:45:55 -0700 | [diff] [blame] | 212 | if (likely(opt->dsthao)) { |
| 213 | offset = ipv6_find_tlv(skb, opt->dsthao, IPV6_TLV_HAO); |
| 214 | if (likely(offset >= 0)) |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 215 | hao = (struct ipv6_destopt_hao *) |
| 216 | (skb_network_header(skb) + offset); |
Masahide NAKAMURA | 70182ed | 2006-08-23 20:45:55 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Arnd Bergmann | 3dd7669 | 2015-09-30 13:26:39 +0200 | [diff] [blame] | 219 | stamp = skb_get_ktime(skb); |
Masahide NAKAMURA | 70182ed | 2006-08-23 20:45:55 -0700 | [diff] [blame] | 220 | |
Arnd Bergmann | 3dd7669 | 2015-09-30 13:26:39 +0200 | [diff] [blame] | 221 | if (!mip6_report_rl_allow(stamp, &ipv6_hdr(skb)->daddr, |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 222 | hao ? &hao->addr : &ipv6_hdr(skb)->saddr, |
Masahide NAKAMURA | 70182ed | 2006-08-23 20:45:55 -0700 | [diff] [blame] | 223 | opt->iif)) |
| 224 | goto out; |
| 225 | |
| 226 | memset(&sel, 0, sizeof(sel)); |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 227 | memcpy(&sel.daddr, (xfrm_address_t *)&ipv6_hdr(skb)->daddr, |
Masahide NAKAMURA | 70182ed | 2006-08-23 20:45:55 -0700 | [diff] [blame] | 228 | sizeof(sel.daddr)); |
| 229 | sel.prefixlen_d = 128; |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 230 | memcpy(&sel.saddr, (xfrm_address_t *)&ipv6_hdr(skb)->saddr, |
Masahide NAKAMURA | 70182ed | 2006-08-23 20:45:55 -0700 | [diff] [blame] | 231 | sizeof(sel.saddr)); |
| 232 | sel.prefixlen_s = 128; |
| 233 | sel.family = AF_INET6; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 234 | sel.proto = fl6->flowi6_proto; |
| 235 | sel.dport = xfrm_flowi_dport(fl, &fl6->uli); |
Masahide NAKAMURA | 70182ed | 2006-08-23 20:45:55 -0700 | [diff] [blame] | 236 | if (sel.dport) |
Al Viro | e69a4ad | 2006-11-14 20:56:00 -0800 | [diff] [blame] | 237 | sel.dport_mask = htons(~0); |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 238 | sel.sport = xfrm_flowi_sport(fl, &fl6->uli); |
Masahide NAKAMURA | 70182ed | 2006-08-23 20:45:55 -0700 | [diff] [blame] | 239 | if (sel.sport) |
Al Viro | e69a4ad | 2006-11-14 20:56:00 -0800 | [diff] [blame] | 240 | sel.sport_mask = htons(~0); |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 241 | sel.ifindex = fl6->flowi6_oif; |
Masahide NAKAMURA | 70182ed | 2006-08-23 20:45:55 -0700 | [diff] [blame] | 242 | |
Alexey Dobriyan | db983c1 | 2008-11-25 17:51:01 -0800 | [diff] [blame] | 243 | err = km_report(net, IPPROTO_DSTOPTS, &sel, |
Masahide NAKAMURA | 70182ed | 2006-08-23 20:45:55 -0700 | [diff] [blame] | 244 | (hao ? (xfrm_address_t *)&hao->addr : NULL)); |
| 245 | |
| 246 | out: |
| 247 | return err; |
| 248 | } |
| 249 | |
Noriaki TAKAMIYA | 3d12689 | 2006-08-23 20:32:34 -0700 | [diff] [blame] | 250 | static int mip6_destopt_init_state(struct xfrm_state *x) |
| 251 | { |
| 252 | if (x->id.spi) { |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 253 | pr_info("%s: spi is not 0: %u\n", __func__, x->id.spi); |
Noriaki TAKAMIYA | 3d12689 | 2006-08-23 20:32:34 -0700 | [diff] [blame] | 254 | return -EINVAL; |
| 255 | } |
| 256 | if (x->props.mode != XFRM_MODE_ROUTEOPTIMIZATION) { |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 257 | pr_info("%s: state's mode is not %u: %u\n", |
| 258 | __func__, XFRM_MODE_ROUTEOPTIMIZATION, x->props.mode); |
Noriaki TAKAMIYA | 3d12689 | 2006-08-23 20:32:34 -0700 | [diff] [blame] | 259 | return -EINVAL; |
| 260 | } |
| 261 | |
| 262 | x->props.header_len = sizeof(struct ipv6_destopt_hdr) + |
| 263 | calc_padlen(sizeof(struct ipv6_destopt_hdr), 6) + |
| 264 | sizeof(struct ipv6_destopt_hao); |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 265 | WARN_ON(x->props.header_len != 24); |
Noriaki TAKAMIYA | 3d12689 | 2006-08-23 20:32:34 -0700 | [diff] [blame] | 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | /* |
| 271 | * Do nothing about destroying since it has no specific operation for |
| 272 | * destination options header unlike IPsec protocols. |
| 273 | */ |
| 274 | static void mip6_destopt_destroy(struct xfrm_state *x) |
| 275 | { |
| 276 | } |
| 277 | |
Ian Morris | cc24bec | 2014-08-24 21:53:11 +0100 | [diff] [blame] | 278 | static const struct xfrm_type mip6_destopt_type = { |
Noriaki TAKAMIYA | 3d12689 | 2006-08-23 20:32:34 -0700 | [diff] [blame] | 279 | .owner = THIS_MODULE, |
Ian Morris | cc24bec | 2014-08-24 21:53:11 +0100 | [diff] [blame] | 280 | .proto = IPPROTO_DSTOPTS, |
Herbert Xu | f04e7e8 | 2007-11-13 21:36:51 -0800 | [diff] [blame] | 281 | .flags = XFRM_TYPE_NON_FRAGMENT | XFRM_TYPE_LOCAL_COADDR, |
Noriaki TAKAMIYA | 3d12689 | 2006-08-23 20:32:34 -0700 | [diff] [blame] | 282 | .init_state = mip6_destopt_init_state, |
| 283 | .destructor = mip6_destopt_destroy, |
| 284 | .input = mip6_destopt_input, |
| 285 | .output = mip6_destopt_output, |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 286 | .reject = mip6_destopt_reject, |
Noriaki TAKAMIYA | 3d12689 | 2006-08-23 20:32:34 -0700 | [diff] [blame] | 287 | }; |
| 288 | |
Noriaki TAKAMIYA | 2c8d7ca | 2006-08-23 20:31:11 -0700 | [diff] [blame] | 289 | static int mip6_rthdr_input(struct xfrm_state *x, struct sk_buff *skb) |
| 290 | { |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 291 | const struct ipv6hdr *iph = ipv6_hdr(skb); |
Noriaki TAKAMIYA | 2c8d7ca | 2006-08-23 20:31:11 -0700 | [diff] [blame] | 292 | struct rt2_hdr *rt2 = (struct rt2_hdr *)skb->data; |
Herbert Xu | 0ebea8e | 2007-11-13 21:45:58 -0800 | [diff] [blame] | 293 | int err = rt2->rt_hdr.nexthdr; |
Noriaki TAKAMIYA | 2c8d7ca | 2006-08-23 20:31:11 -0700 | [diff] [blame] | 294 | |
Herbert Xu | 0ebea8e | 2007-11-13 21:45:58 -0800 | [diff] [blame] | 295 | spin_lock(&x->lock); |
Arnaud Ebalard | d9a9dc6 | 2010-07-16 00:38:44 +0000 | [diff] [blame] | 296 | if (!ipv6_addr_equal(&iph->daddr, (struct in6_addr *)x->coaddr) && |
Noriaki TAKAMIYA | 2c8d7ca | 2006-08-23 20:31:11 -0700 | [diff] [blame] | 297 | !ipv6_addr_any((struct in6_addr *)x->coaddr)) |
Herbert Xu | 0ebea8e | 2007-11-13 21:45:58 -0800 | [diff] [blame] | 298 | err = -ENOENT; |
| 299 | spin_unlock(&x->lock); |
Noriaki TAKAMIYA | 2c8d7ca | 2006-08-23 20:31:11 -0700 | [diff] [blame] | 300 | |
Herbert Xu | 0ebea8e | 2007-11-13 21:45:58 -0800 | [diff] [blame] | 301 | return err; |
Noriaki TAKAMIYA | 2c8d7ca | 2006-08-23 20:31:11 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /* Routing Header type 2 is inserted. |
| 305 | * IP Header's dst address is replaced with Routing Header's Home Address. |
| 306 | */ |
| 307 | static int mip6_rthdr_output(struct xfrm_state *x, struct sk_buff *skb) |
| 308 | { |
| 309 | struct ipv6hdr *iph; |
| 310 | struct rt2_hdr *rt2; |
| 311 | u8 nexthdr; |
| 312 | |
Herbert Xu | 7b277b1 | 2007-10-10 15:44:06 -0700 | [diff] [blame] | 313 | skb_push(skb, -skb_network_offset(skb)); |
Herbert Xu | 007f021 | 2007-10-09 13:25:59 -0700 | [diff] [blame] | 314 | iph = ipv6_hdr(skb); |
Noriaki TAKAMIYA | 2c8d7ca | 2006-08-23 20:31:11 -0700 | [diff] [blame] | 315 | |
Herbert Xu | 007f021 | 2007-10-09 13:25:59 -0700 | [diff] [blame] | 316 | nexthdr = *skb_mac_header(skb); |
| 317 | *skb_mac_header(skb) = IPPROTO_ROUTING; |
Noriaki TAKAMIYA | 2c8d7ca | 2006-08-23 20:31:11 -0700 | [diff] [blame] | 318 | |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 319 | rt2 = (struct rt2_hdr *)skb_transport_header(skb); |
Noriaki TAKAMIYA | 2c8d7ca | 2006-08-23 20:31:11 -0700 | [diff] [blame] | 320 | rt2->rt_hdr.nexthdr = nexthdr; |
| 321 | rt2->rt_hdr.hdrlen = (x->props.header_len >> 3) - 1; |
| 322 | rt2->rt_hdr.type = IPV6_SRCRT_TYPE_2; |
| 323 | rt2->rt_hdr.segments_left = 1; |
| 324 | memset(&rt2->reserved, 0, sizeof(rt2->reserved)); |
| 325 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 326 | WARN_ON(rt2->rt_hdr.hdrlen != 2); |
Noriaki TAKAMIYA | 2c8d7ca | 2006-08-23 20:31:11 -0700 | [diff] [blame] | 327 | |
| 328 | memcpy(&rt2->addr, &iph->daddr, sizeof(rt2->addr)); |
Herbert Xu | b7c6538c | 2007-10-09 13:33:35 -0700 | [diff] [blame] | 329 | spin_lock_bh(&x->lock); |
Noriaki TAKAMIYA | 2c8d7ca | 2006-08-23 20:31:11 -0700 | [diff] [blame] | 330 | memcpy(&iph->daddr, x->coaddr, sizeof(iph->daddr)); |
Herbert Xu | b7c6538c | 2007-10-09 13:33:35 -0700 | [diff] [blame] | 331 | spin_unlock_bh(&x->lock); |
Noriaki TAKAMIYA | 2c8d7ca | 2006-08-23 20:31:11 -0700 | [diff] [blame] | 332 | |
| 333 | return 0; |
| 334 | } |
| 335 | |
Noriaki TAKAMIYA | 2c8d7ca | 2006-08-23 20:31:11 -0700 | [diff] [blame] | 336 | static int mip6_rthdr_init_state(struct xfrm_state *x) |
| 337 | { |
| 338 | if (x->id.spi) { |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 339 | pr_info("%s: spi is not 0: %u\n", __func__, x->id.spi); |
Noriaki TAKAMIYA | 2c8d7ca | 2006-08-23 20:31:11 -0700 | [diff] [blame] | 340 | return -EINVAL; |
| 341 | } |
| 342 | if (x->props.mode != XFRM_MODE_ROUTEOPTIMIZATION) { |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 343 | pr_info("%s: state's mode is not %u: %u\n", |
| 344 | __func__, XFRM_MODE_ROUTEOPTIMIZATION, x->props.mode); |
Noriaki TAKAMIYA | 2c8d7ca | 2006-08-23 20:31:11 -0700 | [diff] [blame] | 345 | return -EINVAL; |
| 346 | } |
| 347 | |
| 348 | x->props.header_len = sizeof(struct rt2_hdr); |
| 349 | |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | /* |
| 354 | * Do nothing about destroying since it has no specific operation for routing |
| 355 | * header type 2 unlike IPsec protocols. |
| 356 | */ |
| 357 | static void mip6_rthdr_destroy(struct xfrm_state *x) |
| 358 | { |
| 359 | } |
| 360 | |
Ian Morris | cc24bec | 2014-08-24 21:53:11 +0100 | [diff] [blame] | 361 | static const struct xfrm_type mip6_rthdr_type = { |
Noriaki TAKAMIYA | 2c8d7ca | 2006-08-23 20:31:11 -0700 | [diff] [blame] | 362 | .owner = THIS_MODULE, |
Ian Morris | cc24bec | 2014-08-24 21:53:11 +0100 | [diff] [blame] | 363 | .proto = IPPROTO_ROUTING, |
Herbert Xu | f04e7e8 | 2007-11-13 21:36:51 -0800 | [diff] [blame] | 364 | .flags = XFRM_TYPE_NON_FRAGMENT | XFRM_TYPE_REMOTE_COADDR, |
Noriaki TAKAMIYA | 2c8d7ca | 2006-08-23 20:31:11 -0700 | [diff] [blame] | 365 | .init_state = mip6_rthdr_init_state, |
| 366 | .destructor = mip6_rthdr_destroy, |
| 367 | .input = mip6_rthdr_input, |
| 368 | .output = mip6_rthdr_output, |
Noriaki TAKAMIYA | 2c8d7ca | 2006-08-23 20:31:11 -0700 | [diff] [blame] | 369 | }; |
| 370 | |
Masahide NAKAMURA | 59fbb3a6 | 2007-06-26 23:56:32 -0700 | [diff] [blame] | 371 | static int __init mip6_init(void) |
Noriaki TAKAMIYA | 2c8d7ca | 2006-08-23 20:31:11 -0700 | [diff] [blame] | 372 | { |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 373 | pr_info("Mobile IPv6\n"); |
Noriaki TAKAMIYA | 2c8d7ca | 2006-08-23 20:31:11 -0700 | [diff] [blame] | 374 | |
Noriaki TAKAMIYA | 3d12689 | 2006-08-23 20:32:34 -0700 | [diff] [blame] | 375 | if (xfrm_register_type(&mip6_destopt_type, AF_INET6) < 0) { |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 376 | pr_info("%s: can't add xfrm type(destopt)\n", __func__); |
Noriaki TAKAMIYA | 3d12689 | 2006-08-23 20:32:34 -0700 | [diff] [blame] | 377 | goto mip6_destopt_xfrm_fail; |
| 378 | } |
Noriaki TAKAMIYA | 2c8d7ca | 2006-08-23 20:31:11 -0700 | [diff] [blame] | 379 | if (xfrm_register_type(&mip6_rthdr_type, AF_INET6) < 0) { |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 380 | pr_info("%s: can't add xfrm type(rthdr)\n", __func__); |
Noriaki TAKAMIYA | 2c8d7ca | 2006-08-23 20:31:11 -0700 | [diff] [blame] | 381 | goto mip6_rthdr_xfrm_fail; |
| 382 | } |
Masahide NAKAMURA | 59fbb3a6 | 2007-06-26 23:56:32 -0700 | [diff] [blame] | 383 | if (rawv6_mh_filter_register(mip6_mh_filter) < 0) { |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 384 | pr_info("%s: can't add rawv6 mh filter\n", __func__); |
Masahide NAKAMURA | 59fbb3a6 | 2007-06-26 23:56:32 -0700 | [diff] [blame] | 385 | goto mip6_rawv6_mh_fail; |
| 386 | } |
| 387 | |
| 388 | |
Noriaki TAKAMIYA | 2c8d7ca | 2006-08-23 20:31:11 -0700 | [diff] [blame] | 389 | return 0; |
| 390 | |
Masahide NAKAMURA | 59fbb3a6 | 2007-06-26 23:56:32 -0700 | [diff] [blame] | 391 | mip6_rawv6_mh_fail: |
| 392 | xfrm_unregister_type(&mip6_rthdr_type, AF_INET6); |
Noriaki TAKAMIYA | 2c8d7ca | 2006-08-23 20:31:11 -0700 | [diff] [blame] | 393 | mip6_rthdr_xfrm_fail: |
Noriaki TAKAMIYA | 3d12689 | 2006-08-23 20:32:34 -0700 | [diff] [blame] | 394 | xfrm_unregister_type(&mip6_destopt_type, AF_INET6); |
| 395 | mip6_destopt_xfrm_fail: |
Noriaki TAKAMIYA | 2c8d7ca | 2006-08-23 20:31:11 -0700 | [diff] [blame] | 396 | return -EAGAIN; |
| 397 | } |
| 398 | |
Masahide NAKAMURA | 59fbb3a6 | 2007-06-26 23:56:32 -0700 | [diff] [blame] | 399 | static void __exit mip6_fini(void) |
Noriaki TAKAMIYA | 2c8d7ca | 2006-08-23 20:31:11 -0700 | [diff] [blame] | 400 | { |
Masahide NAKAMURA | 59fbb3a6 | 2007-06-26 23:56:32 -0700 | [diff] [blame] | 401 | if (rawv6_mh_filter_unregister(mip6_mh_filter) < 0) |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 402 | pr_info("%s: can't remove rawv6 mh filter\n", __func__); |
Florian Westphal | 4f518e8 | 2019-05-03 17:46:19 +0200 | [diff] [blame] | 403 | xfrm_unregister_type(&mip6_rthdr_type, AF_INET6); |
| 404 | xfrm_unregister_type(&mip6_destopt_type, AF_INET6); |
Noriaki TAKAMIYA | 2c8d7ca | 2006-08-23 20:31:11 -0700 | [diff] [blame] | 405 | } |
Masahide NAKAMURA | 59fbb3a6 | 2007-06-26 23:56:32 -0700 | [diff] [blame] | 406 | |
| 407 | module_init(mip6_init); |
| 408 | module_exit(mip6_fini); |
| 409 | |
| 410 | MODULE_LICENSE("GPL"); |
Masahide NAKAMURA | d3d6dd3 | 2007-06-26 23:57:49 -0700 | [diff] [blame] | 411 | MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_DSTOPTS); |
| 412 | MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_ROUTING); |