af_mpls.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767
  1. #include <linux/types.h>
  2. #include <linux/skbuff.h>
  3. #include <linux/socket.h>
  4. #include <linux/sysctl.h>
  5. #include <linux/net.h>
  6. #include <linux/module.h>
  7. #include <linux/if_arp.h>
  8. #include <linux/ipv6.h>
  9. #include <linux/mpls.h>
  10. #include <linux/nospec.h>
  11. #include <linux/vmalloc.h>
  12. #include <net/ip.h>
  13. #include <net/dst.h>
  14. #include <net/sock.h>
  15. #include <net/arp.h>
  16. #include <net/ip_fib.h>
  17. #include <net/netevent.h>
  18. #include <net/netns/generic.h>
  19. #if IS_ENABLED(CONFIG_IPV6)
  20. #include <net/ipv6.h>
  21. #include <net/addrconf.h>
  22. #endif
  23. #include <net/nexthop.h>
  24. #include "internal.h"
  25. /* Maximum number of labels to look ahead at when selecting a path of
  26. * a multipath route
  27. */
  28. #define MAX_MP_SELECT_LABELS 4
  29. #define MPLS_NEIGH_TABLE_UNSPEC (NEIGH_LINK_TABLE + 1)
  30. static int zero = 0;
  31. static int label_limit = (1 << 20) - 1;
  32. static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
  33. struct nlmsghdr *nlh, struct net *net, u32 portid,
  34. unsigned int nlm_flags);
  35. static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
  36. {
  37. struct mpls_route *rt = NULL;
  38. if (index < net->mpls.platform_labels) {
  39. struct mpls_route __rcu **platform_label =
  40. rcu_dereference(net->mpls.platform_label);
  41. rt = rcu_dereference(platform_label[index]);
  42. }
  43. return rt;
  44. }
  45. static inline struct mpls_dev *mpls_dev_get(const struct net_device *dev)
  46. {
  47. return rcu_dereference_rtnl(dev->mpls_ptr);
  48. }
  49. bool mpls_output_possible(const struct net_device *dev)
  50. {
  51. return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
  52. }
  53. EXPORT_SYMBOL_GPL(mpls_output_possible);
  54. static u8 *__mpls_nh_via(struct mpls_route *rt, struct mpls_nh *nh)
  55. {
  56. u8 *nh0_via = PTR_ALIGN((u8 *)&rt->rt_nh[rt->rt_nhn], VIA_ALEN_ALIGN);
  57. int nh_index = nh - rt->rt_nh;
  58. return nh0_via + rt->rt_max_alen * nh_index;
  59. }
  60. static const u8 *mpls_nh_via(const struct mpls_route *rt,
  61. const struct mpls_nh *nh)
  62. {
  63. return __mpls_nh_via((struct mpls_route *)rt, (struct mpls_nh *)nh);
  64. }
  65. static unsigned int mpls_nh_header_size(const struct mpls_nh *nh)
  66. {
  67. /* The size of the layer 2.5 labels to be added for this route */
  68. return nh->nh_labels * sizeof(struct mpls_shim_hdr);
  69. }
  70. unsigned int mpls_dev_mtu(const struct net_device *dev)
  71. {
  72. /* The amount of data the layer 2 frame can hold */
  73. return dev->mtu;
  74. }
  75. EXPORT_SYMBOL_GPL(mpls_dev_mtu);
  76. bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
  77. {
  78. if (skb->len <= mtu)
  79. return false;
  80. if (skb_is_gso(skb) && skb_gso_validate_mtu(skb, mtu))
  81. return false;
  82. return true;
  83. }
  84. EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
  85. static u32 mpls_multipath_hash(struct mpls_route *rt, struct sk_buff *skb)
  86. {
  87. struct mpls_entry_decoded dec;
  88. unsigned int mpls_hdr_len = 0;
  89. struct mpls_shim_hdr *hdr;
  90. bool eli_seen = false;
  91. int label_index;
  92. u32 hash = 0;
  93. for (label_index = 0; label_index < MAX_MP_SELECT_LABELS;
  94. label_index++) {
  95. mpls_hdr_len += sizeof(*hdr);
  96. if (!pskb_may_pull(skb, mpls_hdr_len))
  97. break;
  98. /* Read and decode the current label */
  99. hdr = mpls_hdr(skb) + label_index;
  100. dec = mpls_entry_decode(hdr);
  101. /* RFC6790 - reserved labels MUST NOT be used as keys
  102. * for the load-balancing function
  103. */
  104. if (likely(dec.label >= MPLS_LABEL_FIRST_UNRESERVED)) {
  105. hash = jhash_1word(dec.label, hash);
  106. /* The entropy label follows the entropy label
  107. * indicator, so this means that the entropy
  108. * label was just added to the hash - no need to
  109. * go any deeper either in the label stack or in the
  110. * payload
  111. */
  112. if (eli_seen)
  113. break;
  114. } else if (dec.label == MPLS_LABEL_ENTROPY) {
  115. eli_seen = true;
  116. }
  117. if (!dec.bos)
  118. continue;
  119. /* found bottom label; does skb have room for a header? */
  120. if (pskb_may_pull(skb, mpls_hdr_len + sizeof(struct iphdr))) {
  121. const struct iphdr *v4hdr;
  122. v4hdr = (const struct iphdr *)(hdr + 1);
  123. if (v4hdr->version == 4) {
  124. hash = jhash_3words(ntohl(v4hdr->saddr),
  125. ntohl(v4hdr->daddr),
  126. v4hdr->protocol, hash);
  127. } else if (v4hdr->version == 6 &&
  128. pskb_may_pull(skb, mpls_hdr_len +
  129. sizeof(struct ipv6hdr))) {
  130. const struct ipv6hdr *v6hdr;
  131. v6hdr = (const struct ipv6hdr *)(hdr + 1);
  132. hash = __ipv6_addr_jhash(&v6hdr->saddr, hash);
  133. hash = __ipv6_addr_jhash(&v6hdr->daddr, hash);
  134. hash = jhash_1word(v6hdr->nexthdr, hash);
  135. }
  136. }
  137. break;
  138. }
  139. return hash;
  140. }
  141. static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
  142. struct sk_buff *skb)
  143. {
  144. int alive = ACCESS_ONCE(rt->rt_nhn_alive);
  145. u32 hash = 0;
  146. int nh_index = 0;
  147. int n = 0;
  148. /* No need to look further into packet if there's only
  149. * one path
  150. */
  151. if (rt->rt_nhn == 1)
  152. goto out;
  153. if (alive <= 0)
  154. return NULL;
  155. hash = mpls_multipath_hash(rt, skb);
  156. nh_index = hash % alive;
  157. if (alive == rt->rt_nhn)
  158. goto out;
  159. for_nexthops(rt) {
  160. if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  161. continue;
  162. if (n == nh_index)
  163. return nh;
  164. n++;
  165. } endfor_nexthops(rt);
  166. out:
  167. return &rt->rt_nh[nh_index];
  168. }
  169. static bool mpls_egress(struct mpls_route *rt, struct sk_buff *skb,
  170. struct mpls_entry_decoded dec)
  171. {
  172. enum mpls_payload_type payload_type;
  173. bool success = false;
  174. /* The IPv4 code below accesses through the IPv4 header
  175. * checksum, which is 12 bytes into the packet.
  176. * The IPv6 code below accesses through the IPv6 hop limit
  177. * which is 8 bytes into the packet.
  178. *
  179. * For all supported cases there should always be at least 12
  180. * bytes of packet data present. The IPv4 header is 20 bytes
  181. * without options and the IPv6 header is always 40 bytes
  182. * long.
  183. */
  184. if (!pskb_may_pull(skb, 12))
  185. return false;
  186. payload_type = rt->rt_payload_type;
  187. if (payload_type == MPT_UNSPEC)
  188. payload_type = ip_hdr(skb)->version;
  189. switch (payload_type) {
  190. case MPT_IPV4: {
  191. struct iphdr *hdr4 = ip_hdr(skb);
  192. skb->protocol = htons(ETH_P_IP);
  193. csum_replace2(&hdr4->check,
  194. htons(hdr4->ttl << 8),
  195. htons(dec.ttl << 8));
  196. hdr4->ttl = dec.ttl;
  197. success = true;
  198. break;
  199. }
  200. case MPT_IPV6: {
  201. struct ipv6hdr *hdr6 = ipv6_hdr(skb);
  202. skb->protocol = htons(ETH_P_IPV6);
  203. hdr6->hop_limit = dec.ttl;
  204. success = true;
  205. break;
  206. }
  207. case MPT_UNSPEC:
  208. break;
  209. }
  210. return success;
  211. }
  212. static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
  213. struct packet_type *pt, struct net_device *orig_dev)
  214. {
  215. struct net *net = dev_net(dev);
  216. struct mpls_shim_hdr *hdr;
  217. struct mpls_route *rt;
  218. struct mpls_nh *nh;
  219. struct mpls_entry_decoded dec;
  220. struct net_device *out_dev;
  221. struct mpls_dev *mdev;
  222. unsigned int hh_len;
  223. unsigned int new_header_size;
  224. unsigned int mtu;
  225. int err;
  226. /* Careful this entire function runs inside of an rcu critical section */
  227. mdev = mpls_dev_get(dev);
  228. if (!mdev || !mdev->input_enabled)
  229. goto drop;
  230. if (skb->pkt_type != PACKET_HOST)
  231. goto drop;
  232. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
  233. goto drop;
  234. if (!pskb_may_pull(skb, sizeof(*hdr)))
  235. goto drop;
  236. /* Read and decode the label */
  237. hdr = mpls_hdr(skb);
  238. dec = mpls_entry_decode(hdr);
  239. rt = mpls_route_input_rcu(net, dec.label);
  240. if (!rt)
  241. goto drop;
  242. nh = mpls_select_multipath(rt, skb);
  243. if (!nh)
  244. goto drop;
  245. /* Find the output device */
  246. out_dev = rcu_dereference(nh->nh_dev);
  247. if (!mpls_output_possible(out_dev))
  248. goto drop;
  249. /* Pop the label */
  250. skb_pull(skb, sizeof(*hdr));
  251. skb_reset_network_header(skb);
  252. skb_orphan(skb);
  253. if (skb_warn_if_lro(skb))
  254. goto drop;
  255. skb_forward_csum(skb);
  256. /* Verify ttl is valid */
  257. if (dec.ttl <= 1)
  258. goto drop;
  259. dec.ttl -= 1;
  260. /* Verify the destination can hold the packet */
  261. new_header_size = mpls_nh_header_size(nh);
  262. mtu = mpls_dev_mtu(out_dev);
  263. if (mpls_pkt_too_big(skb, mtu - new_header_size))
  264. goto drop;
  265. hh_len = LL_RESERVED_SPACE(out_dev);
  266. if (!out_dev->header_ops)
  267. hh_len = 0;
  268. /* Ensure there is enough space for the headers in the skb */
  269. if (skb_cow(skb, hh_len + new_header_size))
  270. goto drop;
  271. skb->dev = out_dev;
  272. skb->protocol = htons(ETH_P_MPLS_UC);
  273. if (unlikely(!new_header_size && dec.bos)) {
  274. /* Penultimate hop popping */
  275. if (!mpls_egress(rt, skb, dec))
  276. goto drop;
  277. } else {
  278. bool bos;
  279. int i;
  280. skb_push(skb, new_header_size);
  281. skb_reset_network_header(skb);
  282. /* Push the new labels */
  283. hdr = mpls_hdr(skb);
  284. bos = dec.bos;
  285. for (i = nh->nh_labels - 1; i >= 0; i--) {
  286. hdr[i] = mpls_entry_encode(nh->nh_label[i],
  287. dec.ttl, 0, bos);
  288. bos = false;
  289. }
  290. }
  291. /* If via wasn't specified then send out using device address */
  292. if (nh->nh_via_table == MPLS_NEIGH_TABLE_UNSPEC)
  293. err = neigh_xmit(NEIGH_LINK_TABLE, out_dev,
  294. out_dev->dev_addr, skb);
  295. else
  296. err = neigh_xmit(nh->nh_via_table, out_dev,
  297. mpls_nh_via(rt, nh), skb);
  298. if (err)
  299. net_dbg_ratelimited("%s: packet transmission failed: %d\n",
  300. __func__, err);
  301. return 0;
  302. drop:
  303. kfree_skb(skb);
  304. return NET_RX_DROP;
  305. }
  306. static struct packet_type mpls_packet_type __read_mostly = {
  307. .type = cpu_to_be16(ETH_P_MPLS_UC),
  308. .func = mpls_forward,
  309. };
  310. static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
  311. [RTA_DST] = { .type = NLA_U32 },
  312. [RTA_OIF] = { .type = NLA_U32 },
  313. };
  314. struct mpls_route_config {
  315. u32 rc_protocol;
  316. u32 rc_ifindex;
  317. u8 rc_via_table;
  318. u8 rc_via_alen;
  319. u8 rc_via[MAX_VIA_ALEN];
  320. u32 rc_label;
  321. u8 rc_output_labels;
  322. u32 rc_output_label[MAX_NEW_LABELS];
  323. u32 rc_nlflags;
  324. enum mpls_payload_type rc_payload_type;
  325. struct nl_info rc_nlinfo;
  326. struct rtnexthop *rc_mp;
  327. int rc_mp_len;
  328. };
  329. static struct mpls_route *mpls_rt_alloc(int num_nh, u8 max_alen)
  330. {
  331. u8 max_alen_aligned = ALIGN(max_alen, VIA_ALEN_ALIGN);
  332. struct mpls_route *rt;
  333. rt = kzalloc(ALIGN(sizeof(*rt) + num_nh * sizeof(*rt->rt_nh),
  334. VIA_ALEN_ALIGN) +
  335. num_nh * max_alen_aligned,
  336. GFP_KERNEL);
  337. if (rt) {
  338. rt->rt_nhn = num_nh;
  339. rt->rt_nhn_alive = num_nh;
  340. rt->rt_max_alen = max_alen_aligned;
  341. }
  342. return rt;
  343. }
  344. static void mpls_rt_free(struct mpls_route *rt)
  345. {
  346. if (rt)
  347. kfree_rcu(rt, rt_rcu);
  348. }
  349. static void mpls_notify_route(struct net *net, unsigned index,
  350. struct mpls_route *old, struct mpls_route *new,
  351. const struct nl_info *info)
  352. {
  353. struct nlmsghdr *nlh = info ? info->nlh : NULL;
  354. unsigned portid = info ? info->portid : 0;
  355. int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
  356. struct mpls_route *rt = new ? new : old;
  357. unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
  358. /* Ignore reserved labels for now */
  359. if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
  360. rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
  361. }
  362. static void mpls_route_update(struct net *net, unsigned index,
  363. struct mpls_route *new,
  364. const struct nl_info *info)
  365. {
  366. struct mpls_route __rcu **platform_label;
  367. struct mpls_route *rt;
  368. ASSERT_RTNL();
  369. platform_label = rtnl_dereference(net->mpls.platform_label);
  370. rt = rtnl_dereference(platform_label[index]);
  371. rcu_assign_pointer(platform_label[index], new);
  372. mpls_notify_route(net, index, rt, new, info);
  373. /* If we removed a route free it now */
  374. mpls_rt_free(rt);
  375. }
  376. static unsigned find_free_label(struct net *net)
  377. {
  378. struct mpls_route __rcu **platform_label;
  379. size_t platform_labels;
  380. unsigned index;
  381. platform_label = rtnl_dereference(net->mpls.platform_label);
  382. platform_labels = net->mpls.platform_labels;
  383. for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels;
  384. index++) {
  385. if (!rtnl_dereference(platform_label[index]))
  386. return index;
  387. }
  388. return LABEL_NOT_SPECIFIED;
  389. }
  390. #if IS_ENABLED(CONFIG_INET)
  391. static struct net_device *inet_fib_lookup_dev(struct net *net,
  392. const void *addr)
  393. {
  394. struct net_device *dev;
  395. struct rtable *rt;
  396. struct in_addr daddr;
  397. memcpy(&daddr, addr, sizeof(struct in_addr));
  398. rt = ip_route_output(net, daddr.s_addr, 0, 0, 0);
  399. if (IS_ERR(rt))
  400. return ERR_CAST(rt);
  401. dev = rt->dst.dev;
  402. dev_hold(dev);
  403. ip_rt_put(rt);
  404. return dev;
  405. }
  406. #else
  407. static struct net_device *inet_fib_lookup_dev(struct net *net,
  408. const void *addr)
  409. {
  410. return ERR_PTR(-EAFNOSUPPORT);
  411. }
  412. #endif
  413. #if IS_ENABLED(CONFIG_IPV6)
  414. static struct net_device *inet6_fib_lookup_dev(struct net *net,
  415. const void *addr)
  416. {
  417. struct net_device *dev;
  418. struct dst_entry *dst;
  419. struct flowi6 fl6;
  420. int err;
  421. if (!ipv6_stub)
  422. return ERR_PTR(-EAFNOSUPPORT);
  423. memset(&fl6, 0, sizeof(fl6));
  424. memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
  425. err = ipv6_stub->ipv6_dst_lookup(net, NULL, &dst, &fl6);
  426. if (err)
  427. return ERR_PTR(err);
  428. dev = dst->dev;
  429. dev_hold(dev);
  430. dst_release(dst);
  431. return dev;
  432. }
  433. #else
  434. static struct net_device *inet6_fib_lookup_dev(struct net *net,
  435. const void *addr)
  436. {
  437. return ERR_PTR(-EAFNOSUPPORT);
  438. }
  439. #endif
  440. static struct net_device *find_outdev(struct net *net,
  441. struct mpls_route *rt,
  442. struct mpls_nh *nh, int oif)
  443. {
  444. struct net_device *dev = NULL;
  445. if (!oif) {
  446. switch (nh->nh_via_table) {
  447. case NEIGH_ARP_TABLE:
  448. dev = inet_fib_lookup_dev(net, mpls_nh_via(rt, nh));
  449. break;
  450. case NEIGH_ND_TABLE:
  451. dev = inet6_fib_lookup_dev(net, mpls_nh_via(rt, nh));
  452. break;
  453. case NEIGH_LINK_TABLE:
  454. break;
  455. }
  456. } else {
  457. dev = dev_get_by_index(net, oif);
  458. }
  459. if (!dev)
  460. return ERR_PTR(-ENODEV);
  461. if (IS_ERR(dev))
  462. return dev;
  463. /* The caller is holding rtnl anyways, so release the dev reference */
  464. dev_put(dev);
  465. return dev;
  466. }
  467. static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt,
  468. struct mpls_nh *nh, int oif)
  469. {
  470. struct net_device *dev = NULL;
  471. int err = -ENODEV;
  472. dev = find_outdev(net, rt, nh, oif);
  473. if (IS_ERR(dev)) {
  474. err = PTR_ERR(dev);
  475. dev = NULL;
  476. goto errout;
  477. }
  478. /* Ensure this is a supported device */
  479. err = -EINVAL;
  480. if (!mpls_dev_get(dev))
  481. goto errout;
  482. if ((nh->nh_via_table == NEIGH_LINK_TABLE) &&
  483. (dev->addr_len != nh->nh_via_alen))
  484. goto errout;
  485. RCU_INIT_POINTER(nh->nh_dev, dev);
  486. if (!(dev->flags & IFF_UP)) {
  487. nh->nh_flags |= RTNH_F_DEAD;
  488. } else {
  489. unsigned int flags;
  490. flags = dev_get_flags(dev);
  491. if (!(flags & (IFF_RUNNING | IFF_LOWER_UP)))
  492. nh->nh_flags |= RTNH_F_LINKDOWN;
  493. }
  494. return 0;
  495. errout:
  496. return err;
  497. }
  498. static int mpls_nh_build_from_cfg(struct mpls_route_config *cfg,
  499. struct mpls_route *rt)
  500. {
  501. struct net *net = cfg->rc_nlinfo.nl_net;
  502. struct mpls_nh *nh = rt->rt_nh;
  503. int err;
  504. int i;
  505. if (!nh)
  506. return -ENOMEM;
  507. err = -EINVAL;
  508. /* Ensure only a supported number of labels are present */
  509. if (cfg->rc_output_labels > MAX_NEW_LABELS)
  510. goto errout;
  511. nh->nh_labels = cfg->rc_output_labels;
  512. for (i = 0; i < nh->nh_labels; i++)
  513. nh->nh_label[i] = cfg->rc_output_label[i];
  514. nh->nh_via_table = cfg->rc_via_table;
  515. memcpy(__mpls_nh_via(rt, nh), cfg->rc_via, cfg->rc_via_alen);
  516. nh->nh_via_alen = cfg->rc_via_alen;
  517. err = mpls_nh_assign_dev(net, rt, nh, cfg->rc_ifindex);
  518. if (err)
  519. goto errout;
  520. if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  521. rt->rt_nhn_alive--;
  522. return 0;
  523. errout:
  524. return err;
  525. }
  526. static int mpls_nh_build(struct net *net, struct mpls_route *rt,
  527. struct mpls_nh *nh, int oif, struct nlattr *via,
  528. struct nlattr *newdst)
  529. {
  530. int err = -ENOMEM;
  531. if (!nh)
  532. goto errout;
  533. if (newdst) {
  534. err = nla_get_labels(newdst, MAX_NEW_LABELS,
  535. &nh->nh_labels, nh->nh_label);
  536. if (err)
  537. goto errout;
  538. }
  539. if (via) {
  540. err = nla_get_via(via, &nh->nh_via_alen, &nh->nh_via_table,
  541. __mpls_nh_via(rt, nh));
  542. if (err)
  543. goto errout;
  544. } else {
  545. nh->nh_via_table = MPLS_NEIGH_TABLE_UNSPEC;
  546. }
  547. err = mpls_nh_assign_dev(net, rt, nh, oif);
  548. if (err)
  549. goto errout;
  550. return 0;
  551. errout:
  552. return err;
  553. }
  554. static int mpls_count_nexthops(struct rtnexthop *rtnh, int len,
  555. u8 cfg_via_alen, u8 *max_via_alen)
  556. {
  557. int nhs = 0;
  558. int remaining = len;
  559. if (!rtnh) {
  560. *max_via_alen = cfg_via_alen;
  561. return 1;
  562. }
  563. *max_via_alen = 0;
  564. while (rtnh_ok(rtnh, remaining)) {
  565. struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
  566. int attrlen;
  567. attrlen = rtnh_attrlen(rtnh);
  568. nla = nla_find(attrs, attrlen, RTA_VIA);
  569. if (nla && nla_len(nla) >=
  570. offsetof(struct rtvia, rtvia_addr)) {
  571. int via_alen = nla_len(nla) -
  572. offsetof(struct rtvia, rtvia_addr);
  573. if (via_alen <= MAX_VIA_ALEN)
  574. *max_via_alen = max_t(u16, *max_via_alen,
  575. via_alen);
  576. }
  577. nhs++;
  578. rtnh = rtnh_next(rtnh, &remaining);
  579. }
  580. /* leftover implies invalid nexthop configuration, discard it */
  581. return remaining > 0 ? 0 : nhs;
  582. }
  583. static int mpls_nh_build_multi(struct mpls_route_config *cfg,
  584. struct mpls_route *rt)
  585. {
  586. struct rtnexthop *rtnh = cfg->rc_mp;
  587. struct nlattr *nla_via, *nla_newdst;
  588. int remaining = cfg->rc_mp_len;
  589. int nhs = 0;
  590. int err = 0;
  591. change_nexthops(rt) {
  592. int attrlen;
  593. nla_via = NULL;
  594. nla_newdst = NULL;
  595. err = -EINVAL;
  596. if (!rtnh_ok(rtnh, remaining))
  597. goto errout;
  598. /* neither weighted multipath nor any flags
  599. * are supported
  600. */
  601. if (rtnh->rtnh_hops || rtnh->rtnh_flags)
  602. goto errout;
  603. attrlen = rtnh_attrlen(rtnh);
  604. if (attrlen > 0) {
  605. struct nlattr *attrs = rtnh_attrs(rtnh);
  606. nla_via = nla_find(attrs, attrlen, RTA_VIA);
  607. nla_newdst = nla_find(attrs, attrlen, RTA_NEWDST);
  608. }
  609. err = mpls_nh_build(cfg->rc_nlinfo.nl_net, rt, nh,
  610. rtnh->rtnh_ifindex, nla_via, nla_newdst);
  611. if (err)
  612. goto errout;
  613. if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  614. rt->rt_nhn_alive--;
  615. rtnh = rtnh_next(rtnh, &remaining);
  616. nhs++;
  617. } endfor_nexthops(rt);
  618. rt->rt_nhn = nhs;
  619. return 0;
  620. errout:
  621. return err;
  622. }
  623. static bool mpls_label_ok(struct net *net, unsigned int *index)
  624. {
  625. bool is_ok = true;
  626. /* Reserved labels may not be set */
  627. if (*index < MPLS_LABEL_FIRST_UNRESERVED)
  628. is_ok = false;
  629. /* The full 20 bit range may not be supported. */
  630. if (is_ok && *index >= net->mpls.platform_labels)
  631. is_ok = false;
  632. *index = array_index_nospec(*index, net->mpls.platform_labels);
  633. return is_ok;
  634. }
  635. static int mpls_route_add(struct mpls_route_config *cfg)
  636. {
  637. struct mpls_route __rcu **platform_label;
  638. struct net *net = cfg->rc_nlinfo.nl_net;
  639. struct mpls_route *rt, *old;
  640. int err = -EINVAL;
  641. u8 max_via_alen;
  642. unsigned index;
  643. int nhs;
  644. index = cfg->rc_label;
  645. /* If a label was not specified during insert pick one */
  646. if ((index == LABEL_NOT_SPECIFIED) &&
  647. (cfg->rc_nlflags & NLM_F_CREATE)) {
  648. index = find_free_label(net);
  649. }
  650. if (!mpls_label_ok(net, &index))
  651. goto errout;
  652. /* Append makes no sense with mpls */
  653. err = -EOPNOTSUPP;
  654. if (cfg->rc_nlflags & NLM_F_APPEND)
  655. goto errout;
  656. err = -EEXIST;
  657. platform_label = rtnl_dereference(net->mpls.platform_label);
  658. old = rtnl_dereference(platform_label[index]);
  659. if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
  660. goto errout;
  661. err = -EEXIST;
  662. if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
  663. goto errout;
  664. err = -ENOENT;
  665. if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
  666. goto errout;
  667. err = -EINVAL;
  668. nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len,
  669. cfg->rc_via_alen, &max_via_alen);
  670. if (nhs == 0)
  671. goto errout;
  672. err = -ENOMEM;
  673. rt = mpls_rt_alloc(nhs, max_via_alen);
  674. if (!rt)
  675. goto errout;
  676. rt->rt_protocol = cfg->rc_protocol;
  677. rt->rt_payload_type = cfg->rc_payload_type;
  678. if (cfg->rc_mp)
  679. err = mpls_nh_build_multi(cfg, rt);
  680. else
  681. err = mpls_nh_build_from_cfg(cfg, rt);
  682. if (err)
  683. goto freert;
  684. mpls_route_update(net, index, rt, &cfg->rc_nlinfo);
  685. return 0;
  686. freert:
  687. mpls_rt_free(rt);
  688. errout:
  689. return err;
  690. }
  691. static int mpls_route_del(struct mpls_route_config *cfg)
  692. {
  693. struct net *net = cfg->rc_nlinfo.nl_net;
  694. unsigned index;
  695. int err = -EINVAL;
  696. index = cfg->rc_label;
  697. if (!mpls_label_ok(net, &index))
  698. goto errout;
  699. mpls_route_update(net, index, NULL, &cfg->rc_nlinfo);
  700. err = 0;
  701. errout:
  702. return err;
  703. }
  704. #define MPLS_PERDEV_SYSCTL_OFFSET(field) \
  705. (&((struct mpls_dev *)0)->field)
  706. static const struct ctl_table mpls_dev_table[] = {
  707. {
  708. .procname = "input",
  709. .maxlen = sizeof(int),
  710. .mode = 0644,
  711. .proc_handler = proc_dointvec,
  712. .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
  713. },
  714. { }
  715. };
  716. static int mpls_dev_sysctl_register(struct net_device *dev,
  717. struct mpls_dev *mdev)
  718. {
  719. char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
  720. struct ctl_table *table;
  721. int i;
  722. table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
  723. if (!table)
  724. goto out;
  725. /* Table data contains only offsets relative to the base of
  726. * the mdev at this point, so make them absolute.
  727. */
  728. for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++)
  729. table[i].data = (char *)mdev + (uintptr_t)table[i].data;
  730. snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
  731. mdev->sysctl = register_net_sysctl(dev_net(dev), path, table);
  732. if (!mdev->sysctl)
  733. goto free;
  734. return 0;
  735. free:
  736. kfree(table);
  737. out:
  738. return -ENOBUFS;
  739. }
  740. static void mpls_dev_sysctl_unregister(struct mpls_dev *mdev)
  741. {
  742. struct ctl_table *table;
  743. table = mdev->sysctl->ctl_table_arg;
  744. unregister_net_sysctl_table(mdev->sysctl);
  745. kfree(table);
  746. }
  747. static struct mpls_dev *mpls_add_dev(struct net_device *dev)
  748. {
  749. struct mpls_dev *mdev;
  750. int err = -ENOMEM;
  751. ASSERT_RTNL();
  752. mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
  753. if (!mdev)
  754. return ERR_PTR(err);
  755. err = mpls_dev_sysctl_register(dev, mdev);
  756. if (err)
  757. goto free;
  758. rcu_assign_pointer(dev->mpls_ptr, mdev);
  759. return mdev;
  760. free:
  761. kfree(mdev);
  762. return ERR_PTR(err);
  763. }
  764. static void mpls_ifdown(struct net_device *dev, int event)
  765. {
  766. struct mpls_route __rcu **platform_label;
  767. struct net *net = dev_net(dev);
  768. unsigned int nh_flags = RTNH_F_DEAD | RTNH_F_LINKDOWN;
  769. unsigned int alive;
  770. unsigned index;
  771. platform_label = rtnl_dereference(net->mpls.platform_label);
  772. for (index = 0; index < net->mpls.platform_labels; index++) {
  773. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  774. if (!rt)
  775. continue;
  776. alive = 0;
  777. change_nexthops(rt) {
  778. if (rtnl_dereference(nh->nh_dev) != dev)
  779. goto next;
  780. switch (event) {
  781. case NETDEV_DOWN:
  782. case NETDEV_UNREGISTER:
  783. nh->nh_flags |= RTNH_F_DEAD;
  784. /* fall through */
  785. case NETDEV_CHANGE:
  786. nh->nh_flags |= RTNH_F_LINKDOWN;
  787. break;
  788. }
  789. if (event == NETDEV_UNREGISTER)
  790. RCU_INIT_POINTER(nh->nh_dev, NULL);
  791. next:
  792. if (!(nh->nh_flags & nh_flags))
  793. alive++;
  794. } endfor_nexthops(rt);
  795. WRITE_ONCE(rt->rt_nhn_alive, alive);
  796. }
  797. }
  798. static void mpls_ifup(struct net_device *dev, unsigned int nh_flags)
  799. {
  800. struct mpls_route __rcu **platform_label;
  801. struct net *net = dev_net(dev);
  802. unsigned index;
  803. int alive;
  804. platform_label = rtnl_dereference(net->mpls.platform_label);
  805. for (index = 0; index < net->mpls.platform_labels; index++) {
  806. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  807. if (!rt)
  808. continue;
  809. alive = 0;
  810. change_nexthops(rt) {
  811. struct net_device *nh_dev =
  812. rtnl_dereference(nh->nh_dev);
  813. if (!(nh->nh_flags & nh_flags)) {
  814. alive++;
  815. continue;
  816. }
  817. if (nh_dev != dev)
  818. continue;
  819. alive++;
  820. nh->nh_flags &= ~nh_flags;
  821. } endfor_nexthops(rt);
  822. ACCESS_ONCE(rt->rt_nhn_alive) = alive;
  823. }
  824. }
  825. static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
  826. void *ptr)
  827. {
  828. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  829. struct mpls_dev *mdev;
  830. unsigned int flags;
  831. if (event == NETDEV_REGISTER) {
  832. /* For now just support Ethernet, IPGRE, SIT and IPIP devices */
  833. if (dev->type == ARPHRD_ETHER ||
  834. dev->type == ARPHRD_LOOPBACK ||
  835. dev->type == ARPHRD_IPGRE ||
  836. dev->type == ARPHRD_SIT ||
  837. dev->type == ARPHRD_TUNNEL) {
  838. mdev = mpls_add_dev(dev);
  839. if (IS_ERR(mdev))
  840. return notifier_from_errno(PTR_ERR(mdev));
  841. }
  842. return NOTIFY_OK;
  843. }
  844. mdev = mpls_dev_get(dev);
  845. if (!mdev)
  846. return NOTIFY_OK;
  847. switch (event) {
  848. case NETDEV_DOWN:
  849. mpls_ifdown(dev, event);
  850. break;
  851. case NETDEV_UP:
  852. flags = dev_get_flags(dev);
  853. if (flags & (IFF_RUNNING | IFF_LOWER_UP))
  854. mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
  855. else
  856. mpls_ifup(dev, RTNH_F_DEAD);
  857. break;
  858. case NETDEV_CHANGE:
  859. flags = dev_get_flags(dev);
  860. if (flags & (IFF_RUNNING | IFF_LOWER_UP))
  861. mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
  862. else
  863. mpls_ifdown(dev, event);
  864. break;
  865. case NETDEV_UNREGISTER:
  866. mpls_ifdown(dev, event);
  867. mdev = mpls_dev_get(dev);
  868. if (mdev) {
  869. mpls_dev_sysctl_unregister(mdev);
  870. RCU_INIT_POINTER(dev->mpls_ptr, NULL);
  871. kfree_rcu(mdev, rcu);
  872. }
  873. break;
  874. case NETDEV_CHANGENAME:
  875. mdev = mpls_dev_get(dev);
  876. if (mdev) {
  877. int err;
  878. mpls_dev_sysctl_unregister(mdev);
  879. err = mpls_dev_sysctl_register(dev, mdev);
  880. if (err)
  881. return notifier_from_errno(err);
  882. }
  883. break;
  884. }
  885. return NOTIFY_OK;
  886. }
  887. static struct notifier_block mpls_dev_notifier = {
  888. .notifier_call = mpls_dev_notify,
  889. };
  890. static int nla_put_via(struct sk_buff *skb,
  891. u8 table, const void *addr, int alen)
  892. {
  893. static const int table_to_family[NEIGH_NR_TABLES + 1] = {
  894. AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
  895. };
  896. struct nlattr *nla;
  897. struct rtvia *via;
  898. int family = AF_UNSPEC;
  899. nla = nla_reserve(skb, RTA_VIA, alen + 2);
  900. if (!nla)
  901. return -EMSGSIZE;
  902. if (table <= NEIGH_NR_TABLES)
  903. family = table_to_family[table];
  904. via = nla_data(nla);
  905. via->rtvia_family = family;
  906. memcpy(via->rtvia_addr, addr, alen);
  907. return 0;
  908. }
  909. int nla_put_labels(struct sk_buff *skb, int attrtype,
  910. u8 labels, const u32 label[])
  911. {
  912. struct nlattr *nla;
  913. struct mpls_shim_hdr *nla_label;
  914. bool bos;
  915. int i;
  916. nla = nla_reserve(skb, attrtype, labels*4);
  917. if (!nla)
  918. return -EMSGSIZE;
  919. nla_label = nla_data(nla);
  920. bos = true;
  921. for (i = labels - 1; i >= 0; i--) {
  922. nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
  923. bos = false;
  924. }
  925. return 0;
  926. }
  927. EXPORT_SYMBOL_GPL(nla_put_labels);
  928. int nla_get_labels(const struct nlattr *nla,
  929. u32 max_labels, u8 *labels, u32 label[])
  930. {
  931. unsigned len = nla_len(nla);
  932. unsigned nla_labels;
  933. struct mpls_shim_hdr *nla_label;
  934. bool bos;
  935. int i;
  936. /* len needs to be an even multiple of 4 (the label size) */
  937. if (len & 3)
  938. return -EINVAL;
  939. /* Limit the number of new labels allowed */
  940. nla_labels = len/4;
  941. if (nla_labels > max_labels)
  942. return -EINVAL;
  943. nla_label = nla_data(nla);
  944. bos = true;
  945. for (i = nla_labels - 1; i >= 0; i--, bos = false) {
  946. struct mpls_entry_decoded dec;
  947. dec = mpls_entry_decode(nla_label + i);
  948. /* Ensure the bottom of stack flag is properly set
  949. * and ttl and tc are both clear.
  950. */
  951. if ((dec.bos != bos) || dec.ttl || dec.tc)
  952. return -EINVAL;
  953. switch (dec.label) {
  954. case MPLS_LABEL_IMPLNULL:
  955. /* RFC3032: This is a label that an LSR may
  956. * assign and distribute, but which never
  957. * actually appears in the encapsulation.
  958. */
  959. return -EINVAL;
  960. }
  961. label[i] = dec.label;
  962. }
  963. *labels = nla_labels;
  964. return 0;
  965. }
  966. EXPORT_SYMBOL_GPL(nla_get_labels);
  967. int nla_get_via(const struct nlattr *nla, u8 *via_alen,
  968. u8 *via_table, u8 via_addr[])
  969. {
  970. struct rtvia *via = nla_data(nla);
  971. int err = -EINVAL;
  972. int alen;
  973. if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
  974. goto errout;
  975. alen = nla_len(nla) -
  976. offsetof(struct rtvia, rtvia_addr);
  977. if (alen > MAX_VIA_ALEN)
  978. goto errout;
  979. /* Validate the address family */
  980. switch (via->rtvia_family) {
  981. case AF_PACKET:
  982. *via_table = NEIGH_LINK_TABLE;
  983. break;
  984. case AF_INET:
  985. *via_table = NEIGH_ARP_TABLE;
  986. if (alen != 4)
  987. goto errout;
  988. break;
  989. case AF_INET6:
  990. *via_table = NEIGH_ND_TABLE;
  991. if (alen != 16)
  992. goto errout;
  993. break;
  994. default:
  995. /* Unsupported address family */
  996. goto errout;
  997. }
  998. memcpy(via_addr, via->rtvia_addr, alen);
  999. *via_alen = alen;
  1000. err = 0;
  1001. errout:
  1002. return err;
  1003. }
  1004. static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
  1005. struct mpls_route_config *cfg)
  1006. {
  1007. struct rtmsg *rtm;
  1008. struct nlattr *tb[RTA_MAX+1];
  1009. int index;
  1010. int err;
  1011. err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
  1012. if (err < 0)
  1013. goto errout;
  1014. err = -EINVAL;
  1015. rtm = nlmsg_data(nlh);
  1016. memset(cfg, 0, sizeof(*cfg));
  1017. if (rtm->rtm_family != AF_MPLS)
  1018. goto errout;
  1019. if (rtm->rtm_dst_len != 20)
  1020. goto errout;
  1021. if (rtm->rtm_src_len != 0)
  1022. goto errout;
  1023. if (rtm->rtm_tos != 0)
  1024. goto errout;
  1025. if (rtm->rtm_table != RT_TABLE_MAIN)
  1026. goto errout;
  1027. /* Any value is acceptable for rtm_protocol */
  1028. /* As mpls uses destination specific addresses
  1029. * (or source specific address in the case of multicast)
  1030. * all addresses have universal scope.
  1031. */
  1032. if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
  1033. goto errout;
  1034. if (rtm->rtm_type != RTN_UNICAST)
  1035. goto errout;
  1036. if (rtm->rtm_flags != 0)
  1037. goto errout;
  1038. cfg->rc_label = LABEL_NOT_SPECIFIED;
  1039. cfg->rc_protocol = rtm->rtm_protocol;
  1040. cfg->rc_via_table = MPLS_NEIGH_TABLE_UNSPEC;
  1041. cfg->rc_nlflags = nlh->nlmsg_flags;
  1042. cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
  1043. cfg->rc_nlinfo.nlh = nlh;
  1044. cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
  1045. for (index = 0; index <= RTA_MAX; index++) {
  1046. struct nlattr *nla = tb[index];
  1047. if (!nla)
  1048. continue;
  1049. switch (index) {
  1050. case RTA_OIF:
  1051. cfg->rc_ifindex = nla_get_u32(nla);
  1052. break;
  1053. case RTA_NEWDST:
  1054. if (nla_get_labels(nla, MAX_NEW_LABELS,
  1055. &cfg->rc_output_labels,
  1056. cfg->rc_output_label))
  1057. goto errout;
  1058. break;
  1059. case RTA_DST:
  1060. {
  1061. u8 label_count;
  1062. if (nla_get_labels(nla, 1, &label_count,
  1063. &cfg->rc_label))
  1064. goto errout;
  1065. if (!mpls_label_ok(cfg->rc_nlinfo.nl_net,
  1066. &cfg->rc_label))
  1067. goto errout;
  1068. break;
  1069. }
  1070. case RTA_VIA:
  1071. {
  1072. if (nla_get_via(nla, &cfg->rc_via_alen,
  1073. &cfg->rc_via_table, cfg->rc_via))
  1074. goto errout;
  1075. break;
  1076. }
  1077. case RTA_MULTIPATH:
  1078. {
  1079. cfg->rc_mp = nla_data(nla);
  1080. cfg->rc_mp_len = nla_len(nla);
  1081. break;
  1082. }
  1083. default:
  1084. /* Unsupported attribute */
  1085. goto errout;
  1086. }
  1087. }
  1088. err = 0;
  1089. errout:
  1090. return err;
  1091. }
  1092. static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
  1093. {
  1094. struct mpls_route_config cfg;
  1095. int err;
  1096. err = rtm_to_route_config(skb, nlh, &cfg);
  1097. if (err < 0)
  1098. return err;
  1099. return mpls_route_del(&cfg);
  1100. }
  1101. static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
  1102. {
  1103. struct mpls_route_config cfg;
  1104. int err;
  1105. err = rtm_to_route_config(skb, nlh, &cfg);
  1106. if (err < 0)
  1107. return err;
  1108. return mpls_route_add(&cfg);
  1109. }
  1110. static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
  1111. u32 label, struct mpls_route *rt, int flags)
  1112. {
  1113. struct net_device *dev;
  1114. struct nlmsghdr *nlh;
  1115. struct rtmsg *rtm;
  1116. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
  1117. if (nlh == NULL)
  1118. return -EMSGSIZE;
  1119. rtm = nlmsg_data(nlh);
  1120. rtm->rtm_family = AF_MPLS;
  1121. rtm->rtm_dst_len = 20;
  1122. rtm->rtm_src_len = 0;
  1123. rtm->rtm_tos = 0;
  1124. rtm->rtm_table = RT_TABLE_MAIN;
  1125. rtm->rtm_protocol = rt->rt_protocol;
  1126. rtm->rtm_scope = RT_SCOPE_UNIVERSE;
  1127. rtm->rtm_type = RTN_UNICAST;
  1128. rtm->rtm_flags = 0;
  1129. if (nla_put_labels(skb, RTA_DST, 1, &label))
  1130. goto nla_put_failure;
  1131. if (rt->rt_nhn == 1) {
  1132. const struct mpls_nh *nh = rt->rt_nh;
  1133. if (nh->nh_labels &&
  1134. nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
  1135. nh->nh_label))
  1136. goto nla_put_failure;
  1137. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
  1138. nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
  1139. nh->nh_via_alen))
  1140. goto nla_put_failure;
  1141. dev = rtnl_dereference(nh->nh_dev);
  1142. if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
  1143. goto nla_put_failure;
  1144. if (nh->nh_flags & RTNH_F_LINKDOWN)
  1145. rtm->rtm_flags |= RTNH_F_LINKDOWN;
  1146. if (nh->nh_flags & RTNH_F_DEAD)
  1147. rtm->rtm_flags |= RTNH_F_DEAD;
  1148. } else {
  1149. struct rtnexthop *rtnh;
  1150. struct nlattr *mp;
  1151. int dead = 0;
  1152. int linkdown = 0;
  1153. mp = nla_nest_start(skb, RTA_MULTIPATH);
  1154. if (!mp)
  1155. goto nla_put_failure;
  1156. for_nexthops(rt) {
  1157. rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
  1158. if (!rtnh)
  1159. goto nla_put_failure;
  1160. dev = rtnl_dereference(nh->nh_dev);
  1161. if (dev)
  1162. rtnh->rtnh_ifindex = dev->ifindex;
  1163. if (nh->nh_flags & RTNH_F_LINKDOWN) {
  1164. rtnh->rtnh_flags |= RTNH_F_LINKDOWN;
  1165. linkdown++;
  1166. }
  1167. if (nh->nh_flags & RTNH_F_DEAD) {
  1168. rtnh->rtnh_flags |= RTNH_F_DEAD;
  1169. dead++;
  1170. }
  1171. if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
  1172. nh->nh_labels,
  1173. nh->nh_label))
  1174. goto nla_put_failure;
  1175. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
  1176. nla_put_via(skb, nh->nh_via_table,
  1177. mpls_nh_via(rt, nh),
  1178. nh->nh_via_alen))
  1179. goto nla_put_failure;
  1180. /* length of rtnetlink header + attributes */
  1181. rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
  1182. } endfor_nexthops(rt);
  1183. if (linkdown == rt->rt_nhn)
  1184. rtm->rtm_flags |= RTNH_F_LINKDOWN;
  1185. if (dead == rt->rt_nhn)
  1186. rtm->rtm_flags |= RTNH_F_DEAD;
  1187. nla_nest_end(skb, mp);
  1188. }
  1189. nlmsg_end(skb, nlh);
  1190. return 0;
  1191. nla_put_failure:
  1192. nlmsg_cancel(skb, nlh);
  1193. return -EMSGSIZE;
  1194. }
  1195. static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
  1196. {
  1197. struct net *net = sock_net(skb->sk);
  1198. struct mpls_route __rcu **platform_label;
  1199. size_t platform_labels;
  1200. unsigned int index;
  1201. ASSERT_RTNL();
  1202. index = cb->args[0];
  1203. if (index < MPLS_LABEL_FIRST_UNRESERVED)
  1204. index = MPLS_LABEL_FIRST_UNRESERVED;
  1205. platform_label = rtnl_dereference(net->mpls.platform_label);
  1206. platform_labels = net->mpls.platform_labels;
  1207. for (; index < platform_labels; index++) {
  1208. struct mpls_route *rt;
  1209. rt = rtnl_dereference(platform_label[index]);
  1210. if (!rt)
  1211. continue;
  1212. if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
  1213. cb->nlh->nlmsg_seq, RTM_NEWROUTE,
  1214. index, rt, NLM_F_MULTI) < 0)
  1215. break;
  1216. }
  1217. cb->args[0] = index;
  1218. return skb->len;
  1219. }
  1220. static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
  1221. {
  1222. size_t payload =
  1223. NLMSG_ALIGN(sizeof(struct rtmsg))
  1224. + nla_total_size(4); /* RTA_DST */
  1225. if (rt->rt_nhn == 1) {
  1226. struct mpls_nh *nh = rt->rt_nh;
  1227. if (nh->nh_dev)
  1228. payload += nla_total_size(4); /* RTA_OIF */
  1229. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC) /* RTA_VIA */
  1230. payload += nla_total_size(2 + nh->nh_via_alen);
  1231. if (nh->nh_labels) /* RTA_NEWDST */
  1232. payload += nla_total_size(nh->nh_labels * 4);
  1233. } else {
  1234. /* each nexthop is packed in an attribute */
  1235. size_t nhsize = 0;
  1236. for_nexthops(rt) {
  1237. nhsize += nla_total_size(sizeof(struct rtnexthop));
  1238. /* RTA_VIA */
  1239. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC)
  1240. nhsize += nla_total_size(2 + nh->nh_via_alen);
  1241. if (nh->nh_labels)
  1242. nhsize += nla_total_size(nh->nh_labels * 4);
  1243. } endfor_nexthops(rt);
  1244. /* nested attribute */
  1245. payload += nla_total_size(nhsize);
  1246. }
  1247. return payload;
  1248. }
  1249. static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
  1250. struct nlmsghdr *nlh, struct net *net, u32 portid,
  1251. unsigned int nlm_flags)
  1252. {
  1253. struct sk_buff *skb;
  1254. u32 seq = nlh ? nlh->nlmsg_seq : 0;
  1255. int err = -ENOBUFS;
  1256. skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
  1257. if (skb == NULL)
  1258. goto errout;
  1259. err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
  1260. if (err < 0) {
  1261. /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
  1262. WARN_ON(err == -EMSGSIZE);
  1263. kfree_skb(skb);
  1264. goto errout;
  1265. }
  1266. rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
  1267. return;
  1268. errout:
  1269. if (err < 0)
  1270. rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
  1271. }
  1272. static int resize_platform_label_table(struct net *net, size_t limit)
  1273. {
  1274. size_t size = sizeof(struct mpls_route *) * limit;
  1275. size_t old_limit;
  1276. size_t cp_size;
  1277. struct mpls_route __rcu **labels = NULL, **old;
  1278. struct mpls_route *rt0 = NULL, *rt2 = NULL;
  1279. unsigned index;
  1280. if (size) {
  1281. labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
  1282. if (!labels)
  1283. labels = vzalloc(size);
  1284. if (!labels)
  1285. goto nolabels;
  1286. }
  1287. /* In case the predefined labels need to be populated */
  1288. if (limit > MPLS_LABEL_IPV4NULL) {
  1289. struct net_device *lo = net->loopback_dev;
  1290. rt0 = mpls_rt_alloc(1, lo->addr_len);
  1291. if (!rt0)
  1292. goto nort0;
  1293. RCU_INIT_POINTER(rt0->rt_nh->nh_dev, lo);
  1294. rt0->rt_protocol = RTPROT_KERNEL;
  1295. rt0->rt_payload_type = MPT_IPV4;
  1296. rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
  1297. rt0->rt_nh->nh_via_alen = lo->addr_len;
  1298. memcpy(__mpls_nh_via(rt0, rt0->rt_nh), lo->dev_addr,
  1299. lo->addr_len);
  1300. }
  1301. if (limit > MPLS_LABEL_IPV6NULL) {
  1302. struct net_device *lo = net->loopback_dev;
  1303. rt2 = mpls_rt_alloc(1, lo->addr_len);
  1304. if (!rt2)
  1305. goto nort2;
  1306. RCU_INIT_POINTER(rt2->rt_nh->nh_dev, lo);
  1307. rt2->rt_protocol = RTPROT_KERNEL;
  1308. rt2->rt_payload_type = MPT_IPV6;
  1309. rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
  1310. rt2->rt_nh->nh_via_alen = lo->addr_len;
  1311. memcpy(__mpls_nh_via(rt2, rt2->rt_nh), lo->dev_addr,
  1312. lo->addr_len);
  1313. }
  1314. rtnl_lock();
  1315. /* Remember the original table */
  1316. old = rtnl_dereference(net->mpls.platform_label);
  1317. old_limit = net->mpls.platform_labels;
  1318. /* Free any labels beyond the new table */
  1319. for (index = limit; index < old_limit; index++)
  1320. mpls_route_update(net, index, NULL, NULL);
  1321. /* Copy over the old labels */
  1322. cp_size = size;
  1323. if (old_limit < limit)
  1324. cp_size = old_limit * sizeof(struct mpls_route *);
  1325. memcpy(labels, old, cp_size);
  1326. /* If needed set the predefined labels */
  1327. if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
  1328. (limit > MPLS_LABEL_IPV6NULL)) {
  1329. RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
  1330. rt2 = NULL;
  1331. }
  1332. if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
  1333. (limit > MPLS_LABEL_IPV4NULL)) {
  1334. RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
  1335. rt0 = NULL;
  1336. }
  1337. /* Update the global pointers */
  1338. net->mpls.platform_labels = limit;
  1339. rcu_assign_pointer(net->mpls.platform_label, labels);
  1340. rtnl_unlock();
  1341. mpls_rt_free(rt2);
  1342. mpls_rt_free(rt0);
  1343. if (old) {
  1344. synchronize_rcu();
  1345. kvfree(old);
  1346. }
  1347. return 0;
  1348. nort2:
  1349. mpls_rt_free(rt0);
  1350. nort0:
  1351. kvfree(labels);
  1352. nolabels:
  1353. return -ENOMEM;
  1354. }
  1355. static int mpls_platform_labels(struct ctl_table *table, int write,
  1356. void __user *buffer, size_t *lenp, loff_t *ppos)
  1357. {
  1358. struct net *net = table->data;
  1359. int platform_labels = net->mpls.platform_labels;
  1360. int ret;
  1361. struct ctl_table tmp = {
  1362. .procname = table->procname,
  1363. .data = &platform_labels,
  1364. .maxlen = sizeof(int),
  1365. .mode = table->mode,
  1366. .extra1 = &zero,
  1367. .extra2 = &label_limit,
  1368. };
  1369. ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
  1370. if (write && ret == 0)
  1371. ret = resize_platform_label_table(net, platform_labels);
  1372. return ret;
  1373. }
  1374. static const struct ctl_table mpls_table[] = {
  1375. {
  1376. .procname = "platform_labels",
  1377. .data = NULL,
  1378. .maxlen = sizeof(int),
  1379. .mode = 0644,
  1380. .proc_handler = mpls_platform_labels,
  1381. },
  1382. { }
  1383. };
  1384. static int mpls_net_init(struct net *net)
  1385. {
  1386. struct ctl_table *table;
  1387. net->mpls.platform_labels = 0;
  1388. net->mpls.platform_label = NULL;
  1389. table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
  1390. if (table == NULL)
  1391. return -ENOMEM;
  1392. table[0].data = net;
  1393. net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
  1394. if (net->mpls.ctl == NULL) {
  1395. kfree(table);
  1396. return -ENOMEM;
  1397. }
  1398. return 0;
  1399. }
  1400. static void mpls_net_exit(struct net *net)
  1401. {
  1402. struct mpls_route __rcu **platform_label;
  1403. size_t platform_labels;
  1404. struct ctl_table *table;
  1405. unsigned int index;
  1406. table = net->mpls.ctl->ctl_table_arg;
  1407. unregister_net_sysctl_table(net->mpls.ctl);
  1408. kfree(table);
  1409. /* An rcu grace period has passed since there was a device in
  1410. * the network namespace (and thus the last in flight packet)
  1411. * left this network namespace. This is because
  1412. * unregister_netdevice_many and netdev_run_todo has completed
  1413. * for each network device that was in this network namespace.
  1414. *
  1415. * As such no additional rcu synchronization is necessary when
  1416. * freeing the platform_label table.
  1417. */
  1418. rtnl_lock();
  1419. platform_label = rtnl_dereference(net->mpls.platform_label);
  1420. platform_labels = net->mpls.platform_labels;
  1421. for (index = 0; index < platform_labels; index++) {
  1422. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  1423. RCU_INIT_POINTER(platform_label[index], NULL);
  1424. mpls_notify_route(net, index, rt, NULL, NULL);
  1425. mpls_rt_free(rt);
  1426. }
  1427. rtnl_unlock();
  1428. kvfree(platform_label);
  1429. }
  1430. static struct pernet_operations mpls_net_ops = {
  1431. .init = mpls_net_init,
  1432. .exit = mpls_net_exit,
  1433. };
  1434. static int __init mpls_init(void)
  1435. {
  1436. int err;
  1437. BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
  1438. err = register_pernet_subsys(&mpls_net_ops);
  1439. if (err)
  1440. goto out;
  1441. err = register_netdevice_notifier(&mpls_dev_notifier);
  1442. if (err)
  1443. goto out_unregister_pernet;
  1444. dev_add_pack(&mpls_packet_type);
  1445. rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
  1446. rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
  1447. rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
  1448. err = 0;
  1449. out:
  1450. return err;
  1451. out_unregister_pernet:
  1452. unregister_pernet_subsys(&mpls_net_ops);
  1453. goto out;
  1454. }
  1455. module_init(mpls_init);
  1456. static void __exit mpls_exit(void)
  1457. {
  1458. rtnl_unregister_all(PF_MPLS);
  1459. dev_remove_pack(&mpls_packet_type);
  1460. unregister_netdevice_notifier(&mpls_dev_notifier);
  1461. unregister_pernet_subsys(&mpls_net_ops);
  1462. }
  1463. module_exit(mpls_exit);
  1464. MODULE_DESCRIPTION("MultiProtocol Label Switching");
  1465. MODULE_LICENSE("GPL v2");
  1466. MODULE_ALIAS_NETPROTO(PF_MPLS);