1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * 4 * Authors: 5 * (C) 2016 Pengutronix, Alexander Aring <aar@pengutronix.de> 6 */ 7 8 #include <net/6lowpan.h> 9 #include <net/addrconf.h> 10 #include <net/ndisc.h> 11 12 #include "6lowpan_i.h" 13 14 #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN) 15 #define NDISC_802154_SHORT_ADDR_LENGTH 1 16 static int lowpan_ndisc_parse_802154_options(const struct net_device *dev, 17 struct nd_opt_hdr *nd_opt, 18 struct ndisc_options *ndopts) 19 { 20 switch (nd_opt->nd_opt_len) { 21 case NDISC_802154_SHORT_ADDR_LENGTH: 22 if (ndopts->nd_802154_opt_array[nd_opt->nd_opt_type]) 23 ND_PRINTK(2, warn, 24 "%s: duplicated short addr ND6 option found: type=%d\n", 25 __func__, nd_opt->nd_opt_type); 26 else 27 ndopts->nd_802154_opt_array[nd_opt->nd_opt_type] = nd_opt; 28 return 1; 29 default: 30 /* all others will be handled by ndisc IPv6 option parsing */ 31 return 0; 32 } 33 } 34 35 static int lowpan_ndisc_parse_options(const struct net_device *dev, 36 struct nd_opt_hdr *nd_opt, 37 struct ndisc_options *ndopts) 38 { 39 if (!lowpan_is_ll(dev, LOWPAN_LLTYPE_IEEE802154)) 40 return 0; 41 42 switch (nd_opt->nd_opt_type) { 43 case ND_OPT_SOURCE_LL_ADDR: 44 case ND_OPT_TARGET_LL_ADDR: 45 return lowpan_ndisc_parse_802154_options(dev, nd_opt, ndopts); 46 default: 47 return 0; 48 } 49 } 50 51 static void lowpan_ndisc_802154_update(struct neighbour *n, u32 flags, 52 u8 icmp6_type, 53 const struct ndisc_options *ndopts) 54 { 55 struct lowpan_802154_neigh *neigh = lowpan_802154_neigh(neighbour_priv(n)); 56 u8 *lladdr_short = NULL; 57 58 switch (icmp6_type) { 59 case NDISC_ROUTER_SOLICITATION: 60 case NDISC_ROUTER_ADVERTISEMENT: 61 case NDISC_NEIGHBOUR_SOLICITATION: 62 if (ndopts->nd_802154_opts_src_lladdr) { 63 lladdr_short = __ndisc_opt_addr_data(ndopts->nd_802154_opts_src_lladdr, 64 IEEE802154_SHORT_ADDR_LEN, 0); 65 if (!lladdr_short) { 66 ND_PRINTK(2, warn, 67 "NA: invalid short link-layer address length\n"); 68 return; 69 } 70 } 71 break; 72 case NDISC_REDIRECT: 73 case NDISC_NEIGHBOUR_ADVERTISEMENT: 74 if (ndopts->nd_802154_opts_tgt_lladdr) { 75 lladdr_short = __ndisc_opt_addr_data(ndopts->nd_802154_opts_tgt_lladdr, 76 IEEE802154_SHORT_ADDR_LEN, 0); 77 if (!lladdr_short) { 78 ND_PRINTK(2, warn, 79 "NA: invalid short link-layer address length\n"); 80 return; 81 } 82 } 83 break; 84 default: 85 break; 86 } 87 88 write_lock_bh(&n->lock); 89 if (lladdr_short) { 90 ieee802154_be16_to_le16(&neigh->short_addr, lladdr_short); 91 if (!lowpan_802154_is_valid_src_short_addr(neigh->short_addr)) 92 neigh->short_addr = cpu_to_le16(IEEE802154_ADDR_SHORT_UNSPEC); 93 } 94 write_unlock_bh(&n->lock); 95 } 96 97 static void lowpan_ndisc_update(const struct net_device *dev, 98 struct neighbour *n, u32 flags, u8 icmp6_type, 99 const struct ndisc_options *ndopts) 100 { 101 if (!lowpan_is_ll(dev, LOWPAN_LLTYPE_IEEE802154)) 102 return; 103 104 /* react on overrides only. TODO check if this is really right. */ 105 if (flags & NEIGH_UPDATE_F_OVERRIDE) 106 lowpan_ndisc_802154_update(n, flags, icmp6_type, ndopts); 107 } 108 109 static int lowpan_ndisc_opt_addr_space(const struct net_device *dev, 110 u8 icmp6_type, struct neighbour *neigh, 111 u8 *ha_buf, u8 **ha) 112 { 113 struct lowpan_802154_neigh *n; 114 struct wpan_dev *wpan_dev; 115 int addr_space = 0; 116 117 if (!lowpan_is_ll(dev, LOWPAN_LLTYPE_IEEE802154)) 118 return 0; 119 120 switch (icmp6_type) { 121 case NDISC_REDIRECT: 122 n = lowpan_802154_neigh(neighbour_priv(neigh)); 123 124 read_lock_bh(&neigh->lock); 125 if (lowpan_802154_is_valid_src_short_addr(n->short_addr)) { 126 memcpy(ha_buf, &n->short_addr, 127 IEEE802154_SHORT_ADDR_LEN); 128 read_unlock_bh(&neigh->lock); 129 addr_space += __ndisc_opt_addr_space(IEEE802154_SHORT_ADDR_LEN, 0); 130 *ha = ha_buf; 131 } else { 132 read_unlock_bh(&neigh->lock); 133 } 134 break; 135 case NDISC_NEIGHBOUR_ADVERTISEMENT: 136 case NDISC_NEIGHBOUR_SOLICITATION: 137 case NDISC_ROUTER_SOLICITATION: 138 wpan_dev = lowpan_802154_dev(dev)->wdev->ieee802154_ptr; 139 140 if (lowpan_802154_is_valid_src_short_addr(wpan_dev->short_addr)) 141 addr_space = __ndisc_opt_addr_space(IEEE802154_SHORT_ADDR_LEN, 0); 142 break; 143 default: 144 break; 145 } 146 147 return addr_space; 148 } 149 150 static void lowpan_ndisc_fill_addr_option(const struct net_device *dev, 151 struct sk_buff *skb, u8 icmp6_type, 152 const u8 *ha) 153 { 154 struct wpan_dev *wpan_dev; 155 __be16 short_addr; 156 u8 opt_type; 157 158 if (!lowpan_is_ll(dev, LOWPAN_LLTYPE_IEEE802154)) 159 return; 160 161 switch (icmp6_type) { 162 case NDISC_REDIRECT: 163 if (ha) { 164 ieee802154_le16_to_be16(&short_addr, ha); 165 __ndisc_fill_addr_option(skb, ND_OPT_TARGET_LL_ADDR, 166 &short_addr, 167 IEEE802154_SHORT_ADDR_LEN, 0); 168 } 169 return; 170 case NDISC_NEIGHBOUR_ADVERTISEMENT: 171 opt_type = ND_OPT_TARGET_LL_ADDR; 172 break; 173 case NDISC_ROUTER_SOLICITATION: 174 case NDISC_NEIGHBOUR_SOLICITATION: 175 opt_type = ND_OPT_SOURCE_LL_ADDR; 176 break; 177 default: 178 return; 179 } 180 181 wpan_dev = lowpan_802154_dev(dev)->wdev->ieee802154_ptr; 182 183 if (lowpan_802154_is_valid_src_short_addr(wpan_dev->short_addr)) { 184 ieee802154_le16_to_be16(&short_addr, 185 &wpan_dev->short_addr); 186 __ndisc_fill_addr_option(skb, opt_type, &short_addr, 187 IEEE802154_SHORT_ADDR_LEN, 0); 188 } 189 } 190 191 static void lowpan_ndisc_prefix_rcv_add_addr(struct net *net, 192 struct net_device *dev, 193 const struct prefix_info *pinfo, 194 struct inet6_dev *in6_dev, 195 struct in6_addr *addr, 196 int addr_type, u32 addr_flags, 197 bool sllao, bool tokenized, 198 __u32 valid_lft, 199 u32 prefered_lft, 200 bool dev_addr_generated) 201 { 202 int err; 203 204 /* generates short based address for RA PIO's */ 205 if (lowpan_is_ll(dev, LOWPAN_LLTYPE_IEEE802154) && dev_addr_generated && 206 !addrconf_ifid_802154_6lowpan(addr->s6_addr + 8, dev)) { 207 err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, 208 addr, addr_type, addr_flags, 209 sllao, tokenized, valid_lft, 210 prefered_lft); 211 if (err) 212 ND_PRINTK(2, warn, 213 "RA: could not add a short address based address for prefix: %pI6c\n", 214 &pinfo->prefix); 215 } 216 } 217 #endif 218 219 const struct ndisc_ops lowpan_ndisc_ops = { 220 #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN) 221 .parse_options = lowpan_ndisc_parse_options, 222 .update = lowpan_ndisc_update, 223 .opt_addr_space = lowpan_ndisc_opt_addr_space, 224 .fill_addr_option = lowpan_ndisc_fill_addr_option, 225 .prefix_rcv_add_addr = lowpan_ndisc_prefix_rcv_add_addr, 226 #endif 227 }; 228
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.